multithreading - How to use threading in Python? -
Get link
Facebook
X
Pinterest
Email
Other Apps
I am trying to understand threading in Python. I have seen the documentation and examples, but very clearly, many examples are more sophisticated and I am having trouble understanding them.
How do you clearly illustrate the split tasks for multi-threading? After asking about this question in the
The code below comes from an article / blog post that you should definitely check (no affiliation) - . I will summarize below - just a few lines of code end: multiprocessing.dummy import from pool pool = threadpool (4) result = pool.map (my_function, multiprocessing.dummy) from
My_array) is a multithreaded version of
result = [] for items in my_array: results.append (my_function (item)) Map
The map is a cool little function, and is easily the key to bringing similarity to your Python code. For those unfamiliar people, the map is picked up from some functional languages such as Lisp. It is a function that maps to another function on a sequence.
The map handles the repetition on the sequence for us, applies to the function, and finally stores all the results in an easy list.
< P> implementation
Parallel version of the map function is provided by two libraries: multi-processing, and its very less known, but equally impressive step children: Multiprocessing.dummy
multiprocessing.dummy import import urllib2 as imported threadbuster url [['http://www.python.org', 'http: //www.python .org / about / ',' http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html ',' http://www.python.org/doc/ ', 'Http: // www. Python.org/download/ ',' http://www.python.org/getit/ ',' http://www.python.org/community/ ',' https://wiki.python.org/moin / ',] # Creating pool of labor pool = Threadpool (4) # Open URL in your own thread # and results result = pool. Return map (urllib2.urlopen, urls) # Close pool and eliminate pool Wait for Close () pool.join ()