Python Forum
Problems with variables in multithreading - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problems with variables in multithreading (/thread-36584.html)



Problems with variables in multithreading - Wombaz - Mar-07-2022

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


RE: Problems with variables in multithreading - Larz60+ - Mar-07-2022

see: https://stackoverflow.com/a/19034408


RE: Problems with variables in multithreading - Wombaz - Mar-08-2022

(Mar-07-2022, 09:24 PM)Larz60+ Wrote: see: https://stackoverflow.com/a/19034408
Thanks for the help.