Future of Pypy?
Paul Rubin
no.email at nospam.invalid
Mon Feb 23 23:40:40 EST 2015
Chris Angelico <rosuav at gmail.com> writes:
> So, you would have to pass code to the other process, probably. What
> about this:
> y = 4
> other_thread_queue.put(lambda x: x*y)
the y in the lambda is a free variable that's a reference to the
surrounding mutable context, so that's at best dubious. You could use:
other_thread_queue.put(lambda x, y=y: x*y)
> Or this:
>
> y = [4]
> def next_y():
> y[0] += 1
> return y[0]
> other_thread_queue.put(next_y)
There you have shared mutable data, which isn't allowed in this style.
> It may not be obvious with your squaring example, but every Python
> function has its context (module globals, etc). You can't pass a
> function around without also passing, or sharing, its data.
That is ok as long as the data can't change.
> With threads in a single process, this isn't a problem. They all
> access the same memory space, so they can all share state. As soon as
> you go to separate processes, these considerations become serious.
Right, that's a limitation of processes compared to threads.
More information about the Python-list
mailing list