13

I want to start, from Python, some other Python code, preferably a function, but in another process.

It is mandatory to run this in another process, because I want to run some concurrency tests, like opening a file that was opened exclusively by the parent process (this has to fail).

Requirements:

  • multiplatform: linux, osx, windows
  • compatible with Python 2.6-3.x
asked Jun 15, 2011 at 12:46
2
  • possible duplicate of What's the best way to duplicate fork() in windows? Commented Jun 15, 2011 at 12:50
  • 2
    @AJ that's about Windows, this is about Python. I don't see how it could possibly be a duplicate. Commented Jun 15, 2011 at 12:54

1 Answer 1

25

I would seriously take a look at the documentation for multiprocessing library of Python. From the first sentence of the package's description:

multiprocessing is a package that supports spawning processes using an API similar to the threading module.

It then goes on to say that it side-steps the GIL, which is what it sounds like you're trying to avoid. See their example of a trivial set up:

from multiprocessing import Process
def f(name):
 print 'hello', name
if __name__ == '__main__':
 p = Process(target=f, args=('bob',))
 p.start()
 p.join()

That's a function call being done in another process separate from the process you're inside. Again, all this from the documentation.

answered Jun 15, 2011 at 12:48
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.