[Python-ideas] channel (synchronous queue)

Sturla Molden sturla at molden.no
Sun Feb 19 18:05:57 CET 2012


Den 19.02.2012 17:58, skrev Sturla Molden:
> Den 19.02.2012 17:01, skrev Antoine Pitrou:
>
>> Even for multiprocessing? (I realize we didn't implement a Barrier in 
>> multiprocessing; patches welcome :-)) Regards Antoine. 
>> _______________________________________________ Python-ideas mailing 
>> list Python-ideas at python.org 
>> http://mail.python.org/mailman/listinfo/python-ideas 
>
>
> Here is a sceleton (replace self._data with some IPC mechanism for 
> multiprocessing).
>
> I'll post a barrier for multiprocessing asap, I happen to have one ;-)
>

from multiprocessing import Event
from math import ceil, log

class Barrier(object):

     def __init__(self, numproc):
         self._events = [mp.Event() for n in range(numproc**2)]
         self._numproc = numproc

     def wait(self, rank):

         # loop log2(numproc) times, rounding up
         for k in range(int(ceil(log(self._numproc)/log(2)))):

             # send event to process
             # (rank + 2**k) % numproc
             receiver = (rank + 2**k) % self._numproc
             evt = self._events[rank * self._numproc + receiver]
             evt.set()

             # wait for event from process
             # (rank - 2**k) % numproc
             sender = (rank - 2**k) % self._numproc
             evt = self._events[sender * self._numproc + rank]
             evt.wait()
             evt.clear()






More information about the Python-ideas mailing list