[zeromq-dev] Passing a python object (PyZMQ)

MinRK benjaminrk at gmail.com
Mon Jun 8 20:10:02 CEST 2015


On Mon, Jun 8, 2015 at 11:00 AM, Arnaud Loonstra <arnaud at sphaero.org> wrote:

> A python dictionary is not thread safe, AFAIK. So if multiple
> producers(threads) write to the dictionary you'll run into trouble.
>

Depends what you mean. All of Python is threadsafe, thanks to the GIL.
Since object IDs are unique as long as the objects exist, you should have
no collisions putting different objects in the dictionary from different
threads. You could also key by UUID if for some reason you have multiple
threads sending the same exact object.

-MinRK


>
> Rg,
>
> Arnaud
>
> On June 8, 2015 6:05:08 PM GMT+02:00, Min RK <benjaminrk at gmail.com> wrote:
>
>>
>>
>>  On Jun 8, 2015, at 01:35, Arnaud Loonstra <arnaud at sphaero.org> wrote:
>>>
>>>  On 06/06/2015 01:52 AM, MinRK wrote:
>>>>  Without using ctypes, you could pass the objects through a namespace:
>>>>
>>>>  |# shared namespace
>>>>  ns = {}
>>>>
>>>>  # sender
>>>>  ns[id(obj)] = obj
>>>>  pipe_out.send(struct.pack(b'Q',1))
>>>>
>>>>  # receiver
>>>>  id_bytes = pipe_in.recv()
>>>>  obj_id = struct.unpack(b'Q', id_bytes)[0]
>>>>  obj = ns.pop(obj_id)
>>>>  |
>>>>
>>>>  The ctypes cast approach doesn’t hold a reference to the object while
>>>>  it’s in transit, so it’s possible for the restoration to fail if the
>>>>  object has been garbage collected in between send/recv.
>>>>
>>>
>>>
>>> You're right I ended up doing something very similar to workaround early
>>>  garbage collection but this is much nicer. However it only works between
>>>  a producer and consumer thread, not more. So every producer thread needs
>>>  its own namespace.
>>>
>>
>> I'm not sure why. You should only need one namespace per process.
>>
>> -MinRK
>>
>>
>>>  Rg,
>>>
>>>  Arnaud
>>>  --
>>>  w: http://www.sphaero.org
>>>  t: http://twitter.com/sphaero
>>>  g: http://github.com/sphaero
>>>  i: freenode: sphaero_z25
>>> ------------------------------
>>>
>>>  zeromq-dev mailing list
>>>  zeromq-dev at lists.zeromq.org
>>>  http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>
>> ------------------------------
>>
>> zeromq-dev mailing list
>> zeromq-dev at lists.zeromq.org
>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>
>>
> Send from my feature bloated phone.
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20150608/0917d55e/attachment.htm>


More information about the zeromq-dev mailing list