R
R
Ruguy2018-05-12 00:10:07
Python
Ruguy, 2018-05-12 00:10:07

How to easily parallelize Python code?

Everything that is on the Internet on Cython, Namba, etc. is explained incomprehensibly.
Is there any easy way to parallelize the code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
asd111, 2018-05-12
@asd111

chriskiehl.com/article/parallelism-in-one-line

import urllib2 
from multiprocessing.dummy import Pool as ThreadPool 

urls = [
  '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/'
  # etc.. 
  ]

# Make the Pool of workers
pool = ThreadPool(4) 
# Open the urls in their own threads
# and return the results
results = pool.map(urllib2.urlopen, urls)
#close the pool and wait for the work to finish 
pool.close() 
pool.join()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question