Python Forum

Full Version: Problems with variables in multithreading
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a problem and have no idea how to solve it. So I have n-threads to download data via bluetooth. This data has to go through a handler which unfortunately runs in a new thread of its own, the handler makes its call in C++ to an API. In this handler I want to count up an index. I'm currently trying with threading.local, but that doesn't work because of the new thread that is automatically created. Can anyone help me?

def data_downloader(...):
   points = threading.local()
   point.x = 0
   t = np.zeros((n))
   data = np.zeros((n, 3))

   def handler(p):
        t[point.x] = p.contents.epoch
        data[point.x] = parse_value(p)  
        point.x += 1  # unfortunately, they don't add up here

    callback = FnVoid_VoidP_DataP(lambda ctx, p: handler(p))  # casts the handler to a c++ handler

    get_data(..., callback)  # the data is downloaded here, afterwards the data is processed by the handler
edit: Python version is 3.7
(Mar-07-2022, 09:24 PM)Larz60+ Wrote: [ -> ]see: https://stackoverflow.com/a/19034408
Thanks for the help.