![]() Which is the multithreaded version of: results = Results = pool.map(my_function, my_array) I'll summarize below - it ends up being just a few lines of code: from multiprocessing.dummy import Pool as ThreadPool The code below comes from an article/blog post that you should definitely check out (no affiliation) - Parallelism in one line: A Better Model for Day to Day Threading Tasks. Since this question was asked in 2010, there has been real simplification in how to do simple multithreading with Python with map and pool. This should work for any operating-system, granted your compiler supports this (C++11) feature.On GCC, compile with -std=c++0x -pthread.Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution. Constructs the new thread and runs it. The function we want to execute on the new thread. (Joining means that the thread who invoked the new thread will wait for the new thread to finish execution, before it will continue its own execution). If later on you want to wait for the thread to be done executing the function, call: t1.join() The thread is automatically started upon construction. The constructor's arguments are the function the thread will execute, followed by the function's parameters. ![]() Std::cout to access the std::thread class) Threading.Thread(target=wrapper_targetFunc,Īrgs=(targetFunction, q, otherarg)).start()Ĭreate a function that you want the thread to execute, eg: void task1(std::string msg) If you're insistent about using a function, I'd suggest wrapping your targetFunction with something that knows how to get from the queue. Q.join() # blocks until the queue is empty. # do whatever work you have to do on work Work = self.q.get(timeout=3) # 3s timeout import threadingĭef _init_(self, q, other_arg, *args, **kwargs): I find this easier to read by subclassing Thread, however. The simple way to do this is with a queue.Queue for the work and starting the threads with for _ in range(MAXTHREADS): threading.Thread(target=f, args=(the_queue,)).start().
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |