Python-多处理守护程序 [英] Python- Multiprocessing Daemon

查看:73
本文介绍了Python-多处理守护程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a multiprocess, which creates a csv file. When I run the code with d.daemon = False it works fine, ie it creates a file in the same folder. But when compiled and run with d.daemon = True, it does not, ie does not creates a file. Why's so?

我有一个URL的种子列表,我需要从这些URL抓取数据.

I've a seed list of URLs from which I need to scrap the data.

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)

推荐答案

根据多进程守护程序文档,方法是在脚本结束时将其设置为d.daemon=True,将杀死所有子进程.这是在他们无法开始写之前发生的,因此不会产生任何输出.

According to multiprocess daemon documentation by setting d.daemon=True when your script ends its job will kill all subprocess. That occurs before they can start to write so no output will be produced.

这篇关于Python-多处理守护程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆