[英]Python- Multiprocessing Daemon
I'm creating a multiprocess, which creates a csv file.我正在创建一个多进程,它创建一个 csv 文件。 When I run the code with
d.daemon = False
it works fine, ie it creates a file in the same folder.当我使用
d.daemon = False
运行代码时,它工作正常,即它在同一文件夹中创建了一个文件。 But when compiled and run with d.daemon = True
, it does not, ie does not creates a file.但是当使用
d.daemon = True
编译和运行时,它不会,即不会创建文件。 Why's so?为什么会这样?
I've a seed list of URLs from which I need to scrape the data.我有一个 URL 种子列表,我需要从中抓取数据。
for url in config.SEED_LIST:
# starting a new process for each category.
d = multiprocessing.Process(target=workers.scrape, args=())
d.daemon = True
d.start()
def scrape():
import time
time.sleep(5)
# The above part of code takes some time to scrape 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)
According to multiprocess daemon documentation by setting d.daemon=True
when your script ends its job will kill all subprocess.根据多进程守护进程文档,通过在脚本结束时设置
d.daemon=True
其工作将终止所有子进程。 That occurs before they can start to write so no output will be produced.这发生在他们开始写入之前,因此不会产生任何输出。
d.daemon = True
means that the subprocess is automatically terminated after the parent process ends to prevent orphan processes. d.daemon = True
表示子进程在父进程结束后自动终止,防止出现孤儿进程。 join()
is helpful by simply adding d.join()
after d.start()
, so that the parent process does not end before the child process; join()
通过在d.join()
之后简单地添加d.join()
d.start()
,这样父进程就不会在子进程之前结束; instead, the parent process will wait until the child process ends.相反,父进程将等到子进程结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.