3

I am using Python 2.7. I want to create an instance of an object, and call a method within it in a separate process. Something like this:

import subprocesss
class A
   def doAwork(self, text):
      print text

class B
   def doBWork
      aInst = A()
      subprocess(A.dowork("called from doBwork"))

Can this be done, or, do I need to turn around and call python as a subprocesss?

Edit: I can not use threads for this. The method called in aInst in reality loads a non-thread safe dll.

Thanks

3 Answers 3

4

You have to use the multiprocessing module instead of subprocess

Simple example copied from documentation link above:

from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

With your example it would look like this:

from multiprocessing import Process

class A:
   def doAwork(self, text):
      print text

class B:
   def doBweork(self):
      aInst = A()
      p = Process(target=aInst.doAwork("called from doBWork")
      p.start()
      p.join()
Sign up to request clarification or add additional context in comments.

4 Comments

I probably could have stated my original problem with more detail. But, in reality, I am calling into this class from a C++ via the python.h header and PyImport_ImportModule(), etc. This is really strange. But, when I call p.start() as above, another instance of my calling exe is created.
Isn't that correct? I thought you wanted the code to execute in another (sub)process.
I have a C++ exe creating the python instance. The python instance needs to turn around and create a new process to execute the python code. Instead, a new C++ exe is launched so that I have 2 of them instead of a new process running aInst.doWork()
Oh that's strange. I see now the documentation states this: Functionality within this package requires that the __main__ module be importable by the children. Probably the main module is in this case your C++ program, and so it gets started. But I have not used python from C++ yet, so I can't answer this now. (If I find something I will update my answer)
0

multiprocessing http://docs.python.org/2/library/multiprocessing.html The Process class will give you all you need

Comments

0

subprocess is used to spawn a process (EXE on windows) and not a function. In this case, you may use threads:

import thread

class A:
   def doAwork(self, text):
      print text

class B:
   def doBwork(self):
      aInst = A()
      thread.start_new_thread(aInst.doAwork, ("called from doBwork",))

B().doBwork()

1 Comment

I can't do in thread. I specifically need separate processes because the actual code loads a dll that is not thread safe. I will update the question to reflect this requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.