Skip to content

Instantly share code, notes, and snippets.

@seozed
Last active August 16, 2018 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seozed/fb5c4ff4f581b5981771e02897592f55 to your computer and use it in GitHub Desktop.
Save seozed/fb5c4ff4f581b5981771e02897592f55 to your computer and use it in GitHub Desktop.
高度抽象的多线程与多进程库
"""
多进程示例
"""
import time
from multiprocessing import Pool
def run(fn):
time.sleep(1)
print(fn)
if __name__ == "__main__":
testFL = range(30)
pool = Pool(5) # 可以同时跑N个进程
pool.map(run, testFL)
pool.close()
pool.join()
"""
多线程
"""
import requests
from multiprocessing.dummy import Pool as ThreadPool
urls = [
'https://www.baidu.com/?1',
'https://www.baidu.com/?1',
'https://www.baidu.com/?1',
'https://www.baidu.com/?1',
'https://www.baidu.com/?1',
'https://www.baidu.com/?1',
'https://www.baidu.com/?1',
]
pool = ThreadPool(10)
results = pool.map(requests.get, urls)
pool.close()
pool.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment