Last active
August 16, 2018 09:29
-
-
Save seozed/fb5c4ff4f581b5981771e02897592f55 to your computer and use it in GitHub Desktop.
高度抽象的多线程与多进程库
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
多进程示例 | |
""" | |
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