ctypes and how to copy data passed to callback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • waldek

    ctypes and how to copy data passed to callback

    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(d ata), but it does not work - I got nothing.
    Any idea why it's happening ?

    --------------------- main thread --------
    def callback(data, size):
    myqueue.put((da ta, size))

    mydll = cdll.MyDLL
    cbproto = CFUNCTYPE(c_int , POINTER(c_char) , c_int)
    mycallback = cbproto(callbac k)

    mydll.RegisterC allback(mycallb ack)

    ---------------------------------- thread listener
    ----------------------

    while True:
    data, size = myqueue.get()
    print "***", data[:size]

    ------------------------------------------------------------------------------
  • Diez B. Roggisch

    #2
    Re: ctypes and how to copy data passed to callback

    waldek wrote:
    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(d ata), but it does not work - I got nothing.
    Any idea why it's happening ?
    >
    --------------------- main thread --------
    def callback(data, size):
    myqueue.put((da ta, size))
    >
    mydll = cdll.MyDLL
    cbproto = CFUNCTYPE(c_int , POINTER(c_char) , c_int)
    mycallback = cbproto(callbac k)
    >
    mydll.RegisterC allback(mycallb ack)
    >
    ---------------------------------- thread listener
    ----------------------
    >
    while True:
    data, size = myqueue.get()
    print "***", data[:size]
    >
    ------------------------------------------------------------------------------
    You need to allocate e.g. a bytebuffer using ctypes and then copy the memory
    area you get passed into that buffer.

    Otherwise I presume whoever invokes the callback releases the originaly
    memory block after the callback terminated.

    Diez

    Comment

    • Thomas Heller

      #3
      Re: ctypes and how to copy data passed to callback

      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(d ata), but it does not work - I got nothing.
      Any idea why it's happening ?
      >
      --------------------- main thread --------
      def callback(data, size):
      myqueue.put((da ta, size))
      >
      mydll = cdll.MyDLL
      cbproto = CFUNCTYPE(c_int , POINTER(c_char) , c_int)
      mycallback = cbproto(callbac k)
      >
      mydll.RegisterC allback(mycallb ack)
      >
      ---------------------------------- 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(dat a[:size])
      while True:
      data = myqueue.get()
      print "***", data
      Thomas

      Comment

      • waldek

        #4
        Re: ctypes and how to copy data passed to callback

        On Jul 28, 4:03 pm, Thomas Heller <thel...@python .netwrote:
        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(d ata), but it does not work - I got nothing.
        Any idea why it's happening ?
        >
        --------------------- main thread --------
        def callback(data, size):
        myqueue.put((da ta, size))
        >
        mydll = cdll.MyDLL
        cbproto = CFUNCTYPE(c_int , POINTER(c_char) , c_int)
        mycallback = cbproto(callbac k)
        >
        mydll.RegisterC allback(mycallb ack)
        >
        ---------------------------------- 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(dat a[:size])
        while True:
        data = myqueue.get()
        print "***", data
        >
        Thomas
        Both solutions work fine. The secon is nicer :)

        Thanks

        Comment

        Working...