0

I would like to do something like this:

import MyProcess

proc1 = MyProcess('python').start()
proc2 = MyProcess('bash').start()

### This would mimic what happens in a python shell
print(proc1('a=10')) ==> ___
print(proc1('a*2')) ==> 20
proc1('b=a*2')
print(proc1("print(b)") ==> 20

### This would mimic what happens in a bash shell
proc2("a=hello")
proc2("b=there")
c = proc2("echo \"$a $b\"")
print(c) ==> hello there

proc1.stop()
proc2.stop()

I'm not really sure where to start. I tried using subprocess but as soon as a read back the stdout from my last command (issued with process.stdin.write, the process seems to quit and won't execute any further commands. Also, multiprocessing might be better as it can make better use of resources like multiple cores.

I can't find any examples of multiprocessing where you start a process, issue a command, get the output, issue another command, get that output, etc. It seems like it's always about starting the process and then letting it finish. Would I need the process to wrap some kind of while True loop?

abalter
  • 9,663
  • 17
  • 90
  • 145
  • `multiprocessing` is used to run multiple Python processes, `subprocess` to run a "foreign" process from the Python program. What is your use case? Do you want both? And to what extent will your processes need to communicate? – JohanL Feb 25 '18 at 09:20
  • @JohanL I would like to run both Python and foreign process (say R, bash, ...), but they would all be shells (or kernels?). Communication is not a requirement. If possible, I would like to be able to read the values of variables in the Python and foreign processes. Will subprocess make use of multiple cores? – abalter Feb 25 '18 at 15:54
  • Well, when it comes to process scheduling you are in the hands of the operating system but there is nothing that prevents subprocess to run on different cores, so most likely it will. – JohanL Feb 25 '18 at 16:21
  • I'm wanting to implement something like Jupyter notebooks that can have one cell that calls a python kernel and another that calls an R kernel and another that calls a bash process. – abalter Feb 25 '18 at 22:15
  • And running the framework in a Python? It's quite a project, particular handling graphics. How far have you come? – JohanL Feb 25 '18 at 22:43
  • Well, I've come to the part where I don't even know how to initiate and communicate multiple background processes :) Right now, I'm not trying to make a notebook. I'm almost embarrassed to put this link up at this stage of development: https://github.com/abalter/polyglottus – abalter Feb 26 '18 at 03:34

0 Answers0