ctypes and how to copy data passed to callback

Thomas Heller theller at python.net
Mon Jul 28 16:03:15 CEST 2008


waldek schrieb:
> Hi,
> 
> I'm trying  to handle data passed to Py Callback which is called from
> C dll. Callback passes data to another thread using Queue module and
> there the data are printed out.
> 
> If data is printed out in a callback itself  it's ok. If I put on
> queue and next get from queue in another thread script prints some
> trash. Looks like the data is released when callback returned. I tired
> to make d = copy.deepcopy(data), but it does not work - I got nothing.
> Any idea why it's happening ?
> 
> --------------------- main thread  --------
> def callback(data, size):
>     myqueue.put((data, size))
> 
> mydll = cdll.MyDLL
> cbproto = CFUNCTYPE(c_int, POINTER(c_char), c_int)
> mycallback = cbproto(callback)
> 
> mydll.RegisterCallback(mycallback)
> 
> ---------------------------------- thread listener
> ----------------------
> 
> while True:
>     data, size = myqueue.get()
>     print "***", data[:size]
> 
> ------------------------------------------------------------------------------

I guess your code would work if you change it in this way:

> def callback(data, size):
>     myqueue.put(data[:size])

> while True:
>     data = myqueue.get()
>     print "***", data

Thomas



More information about the Python-list mailing list