multithreading - How to use threading in Python? -


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.

Enter image details here


< 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 ()

More time results:

  Single thread: 14.4 sec 4 pool: 3.1 sec 8 pool: 1.4 sec 13 Pool: 1.3 seconds  

Pass multiple arguments (This only works for Python 3.3 and later): ():

To pass multiple arrays:

Or to pass a constant and an array:

 
  result = Pool.starmap (function, zip (list_a, list_b))   results = pool.starmap (function, zip (itertools.repeat (continuous), list_a))  
< P> If you were before Python Sector version you are using, so you can pass as many arguments through.

(Thanks for the useful comment)