运行下载图片多进程脚本如下:
from multiprocessing import Queue, Process
import requests
def download(q: Queue):
while True:
url = q.get()
r = requests.get(url)
img_name = url.split('/')[-1]
with open(img_name, 'wb') as f:
f.write(r.content)
if q.empty():
break
if __name__ == '__main__':
tasks = Queue()
tasks.put("https://static.runoob.com/images/icon/mobile-icon.png")
tasks.put("https://www.runoob.com/wp-content/themes/runoob/assets/images/qrcode.png")
p1 = Process(target=download, args=(tasks,))
p1.start()
p1.join()
报错如下:
objc[57171]: +[__NSPlaceholderDate initialize] may have been in progress in another thread when fork() was called.
解决如下:
Pycharm顶部工具栏Run->Edit Configurations->Environment variables-> Name: OBJC_DISABLE_INITIALIZE_FORK_SAFETY Value:YES-> OK-> Apply-> OK
运行成功