擅长:python、mysql、java
<p>要在多个进程中执行并行执行,请查看<a href="https://docs.python.org/3.4/library/multiprocessing.html#multiprocessing.pool.Pool" rel="nofollow">multiprocessing</a>模块。在</p>
<p>下面的代码是一个简单的示例,它为数组中的每个url启动一个进程。在实践中(如果url的数量任意大),您可能希望使用<a href="https://docs.python.org/3.4/library/multiprocessing.html#multiprocessing.pool.Pool" rel="nofollow">Pool</a>,这样就可以将url排队到固定数量的进程中。在</p>
<pre><code>from multiprocessing import Process
def worker_process(url):
# process url...
print 'processing %s' % url
def main():
urls = ['http://www1.example.com/', 'http://www2.example.com/']
workers = []
for i in range(0, len(urls)):
p = Process(target=worker_process, args=(urls[i],))
p.start()
workers.append(p)
for worker in workers:
worker.join()
if __name__ == '__main__':
main()
</code></pre>