记Mac上Pycharm运行多进程脚本报错[may have been in progress in another thread when fork() was called]

运行下载图片多进程脚本如下:

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

运行成功
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/z12347891/article/details/128378225
今日推荐