There are certain guidelines and idioms which should be adhered to when using the processing package.
As far as possible one should try to avoid shifting large amounts of data between processes.
It is probably best to stick to using queues or pipes for communication between processes rather than using the lower level synchronization primitives from the threading module.
Do not use a proxy object from more than one thread unless you protect it with a lock.
Alternatively another copy of the proxy can be created using the copy.copy() function.
(There is never a problem with different processes using the 'same' proxy.)
On Windows many of types from the processing package need to be picklable so that child processes can use them. However, on Unix the following types are not picklable:
Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Queue, SharedValue, SharedStruct, SharedArray.
For the sake of compatibility it is better not to rely on these types being picklable.
Platforms such as Windows which lack os.fork() have a few extra restrictions:
Ensure that all arguments to Process.__init__() are picklable.
Also, if you subclass Process then make sure that instances will be picklable when the start() method is called.
Bear in mind that if code run in a child process tries to access a global variable, then the value it sees (if any) may not be the same as the value in the parent process at the time that start() was called.
However, global variables which are just module level constants cause no problems.
Make sure that the module containing the target of a Process instance (or the definition of a subclass of Process you are using) can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process).
For example, under Windows running the following module would recursively create new processes until you run out of memory or get a crash:
from processing import Process def foo(): print 'hello' p = Process(target=foo) p.start()
Instead one should protect creation of the new process by using if __name__ == '__main__': as follows:
from processing import Process def foo(): print 'hello' if __name__ == '__main__': p = Process(target=foo) p.start()
This allows the newly spawned Python interpreter to safely import the module and then run the module's foo() function.
One can produce Windows executables from a python program by using py2exe, PyInstaller, cx_Freeze etc. However, if the program uses processing then one needs to call freezeSupport() immediately after the if __name__ == '__main__': line of the main module. Otherwise one will probably get the same problems mentioned above concerning Safe importing. For example
from processing import Process, freezeSupport def foo(): print 'hello' if __name__ == '__main__': freezeSupport() p = Process(target=foo) p.start()