How to run some python code in another process?

I want to start with Python, another Python code, preferably with a function, but in a different process .

Be sure to run this in another process, because I want to run several concurrency tests, for example, open a file that was opened exclusively by the parent process (this should fail).

Requirements:

  • multi-platform: linux, osx, windows
  • compatible with Python 2.6-3.x
+7
source share
1 answer

I would seriously consider the documentation for multiprocessing the Python library. From the first sentence of the package description:

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

He then goes on to say that he is following the GIL steps, which is similar to what you are trying to avoid. See Their trivial setup example:

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

This is a function call executed in another process, separate from the process you are in. Again, all this is from the documentation.

+11
source

All Articles