Python-多处理守护进程

2024-05-15 06:28:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个多进程,它创建一个csv文件。当我用d.daemon = False运行代码时,它工作正常,即在同一文件夹中创建一个文件。但是当使用d.daemon = True编译和运行时,它不会创建文件。为什么是这样?

我的代码

我有一个url种子列表,需要从中删除数据。

for url in config.SEED_LIST:
    # starting a new process for each category.
    d = multiprocessing.Process(target=workers.scrap, args=())
    d.daemon = True
    d.start()


def scrap():
    import time
    time.sleep(5)
    # The above part of code takes some time to scrap a webpage, applying
    # some logic, which takes some time to execute, hence I've added a time
    # sleep of 5 secs. But when run with daemon = True, the file is not
    # created. Else it works fine.

    data = [[1, 2, 3, 4], [2224, 34, 34, 34, 34]]
    with open('1.csv', "wb") as f:
        writer = csv.writer(f)
        writer.writerows(data)

Tags: 文件ofcsvto代码trueurlfor

热门问题