Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25544

Re: multiprocessing: apply_async with different callbacks

Date 2012-07-18 01:17 +0200
From André Panisson <panisson@di.unito.it>
Subject Re: multiprocessing: apply_async with different callbacks
References <5005DCBB.4080205@di.unito.it>
Newsgroups comp.lang.python
Message-ID <mailman.2252.1342567044.4697.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 07/17/2012 11:44 PM, André Panisson wrote:
> Hi all,
>
> I'm having a strange behavior when executing the following script:
> ---------------------------
> import multiprocessing
>
> def f(i):
>     return i
>
> p = multiprocessing.Pool()
> for i in range(20):
>     def c(r):
>         print r, i
>     p.apply_async(f, (i,) , callback=c)
> p.close()
> p.join()
> ---------------------------
>
> Result:
> 0 6
> 1 11
> 2 13
> 3 15
> 4 15
> 5 19
> etc....
>
> It seems that the callbacks of all submitted tasks are being 
> overridden with the last callback submitted by apply_async.
> Is this the right behaviour or I am stumbling in some issue? I'm using 
> Python 2.7.3 @ Ubuntu 12.04
>
> Regards,


Sorry for taking the time, I just found the solution:
-------------------
import multiprocessing

def f(i):
     return i

def cfactory(i):
     def c(r):
         print r, i
     return c

p = multiprocessing.Pool()
for i in range(20):
     p.apply_async(f, (i,) , callback=cfactory(i))
p.close()
p.join()
------------------

Regards,
André

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: multiprocessing: apply_async with different callbacks André Panisson <panisson@di.unito.it> - 2012-07-18 01:17 +0200

csiph-web