[Python-Dev] Cloning threading.py using proccesses

Richard Oudkerk r.m.oudkerk at googlemail.com
Mon Oct 9 13:59:06 CEST 2006


I am not sure how sensible the idea is, but I have had a first stab at
writing a module processing.py which is a near clone of threading.py
but uses processes and sockets for communication.  (It is one way of
avoiding the GIL.)

I have tested it on unix and windows and it seem to work pretty well.
(Getting round the lack of os.fork on windows is a bit awkward.)
There is also another module dummy_processing.py which has the same
api but is just a wrapper round threading.py.

Queues, Locks, RLocks, Conditions, Semaphores and some other shared
objects are implemented.

People are welcome to try out the tests in test_processing.py
contained in the zipfile.  More information is included in the README
file.

As a quick example, the code

.   from processing import Process, Queue, ObjectManager
.
.   def f(token):
.       q = proxy(token)
.       for i in range(10):
.           q.put(i*i)
.       q.put('STOP')
.
.   if __name__ == '__main__':
.       manager = ObjectManager()
.       token = manager.new(Queue)
.       queue = proxy(token)
.
.       t = Process(target=f, args=[token])
.       t.start()
.
.       result = None
.       while result != 'STOP':
.           result = queue.get()
.           print result
.
.       t.join()

is not very different from the normal threaded equivalent

.   from threading import Thread
.   from Queue import Queue
.
.   def f(q):
.       for i in range(10):
.           q.put(i*i)
.       q.put('STOP')
.
.   if __name__ == '__main__':
.       queue = Queue()
.
.       t = Thread(target=f, args=[queue])
.       t.start()
.
.       result = None
.       while result != 'STOP':
.           result = queue.get()
.           print result
.
.       t.join()

Richard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: processing.zip
Type: application/zip
Size: 16648 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-dev/attachments/20061009/e707ab8d/attachment-0001.zip 


More information about the Python-Dev mailing list