![]() ![]() # the functions you want to execute on each message - these were your threads ![]() Sync_print('%s:got %s' % (threading.current_thread().name, data)) # create tasks of form: (function, message) Sync_print("Manager got message %s" % message) Sync_print("Manager waiting for message") Pool = ThreadPool(min(len(functions), MAX_THREADS)) # the manager actually holds a pool of threads ( Note: python 3 has Barriers which may become handy here if you choose another implementation): #!/usr/bin/pythonįrom multiprocessing.pool import ThreadPool The idea is to create tasks with the function and the message and submit them to a pool of threads to execute. Tm = threading.Thread(name='main', target=main)įor your follow up question in the comments, here is a mechanism with fixed number of synchronization primitives. T2 = threading.Thread(name='func2', target=func2) T1 = threading.Thread(name='func1', target=func1) So I still want to know if there is a better idea.Ĭan you use a queue per thread? If so, you could simply publish to each thread using it's own queue: #!/usr/bin/python The problem is that if the number of threads is N, TSignal needs to maintain N queues, and TSignal.put calls Queue.put N times actually. In a putter function, just use put as same as Queue.put(): signal.put(data) Then use queue in the same manner as Queue.get(): data= queue.get() In a getter function, insert a with statement like: with signal.NewQueue() as queue: Queue= self.TQueue(self,idx,self.queues)ĭef put(self,item,block=True,timeout=None):įor idx,queue in ():ĭef _exit_(self,e_type,e_value,e_traceback): Here I expect Func1 and Func2 get the same data from MainThread, but only one of Func1 and Func2 can get the data.īased on the Reut Sharabani's idea, I implemented a signal class. Tm= threading.Thread(name='main', target=MainThread) T2= threading.Thread(name='func2', target=Func2) ![]() T1= threading.Thread(name='func1', target=Func1) I want to do: a thread sends a data with Queue.put(xxx), and some threads get the same data. How can I use Queue.get() from multiple threads in Python? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |