2
有人可以解释为什么线程在多处理.Process中不起作用。Python,使用多处理线程
我附上了一些例子来解释我的问题。
我有一个每秒执行一次并写入文件的进程。当我从shell运行它时,它按预期工作。
stat_collect.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from threading import Timer
from os import path
from datetime import datetime
STAT_DATETIME_FMT = '%Y-%m-%d %H:%M:%S'
def collect_statistics():
my_file = 'test.file'
if not path.exists(my_file):
with open(my_file, 'w') as fp:
fp.write(datetime.now().strftime(STAT_DATETIME_FMT) + '\n')
else:
with open(my_file, 'a') as fp:
fp.write(datetime.now().strftime(STAT_DATETIME_FMT) + '\n')
Timer(1, collect_statistics).start()
if __name__ == '__main__':
collect_statistics()
当我尝试从其他脚本运行(在后台运行):只执行一次
#!/usr/bin/env python
from multiprocessing import Process
from stat_collect import collect_statistics # logger sc
if __name__ == '__main__':
# This don't work
p = Process(target=collect_statistics)
p.start()
while True:
pass
方法collect_statistics,但如果我用螺纹(target = collect_statistics).start()它就像我从shell运行一样。为什么会发生?
那么,调用Process与从命令行运行stat_collect.py不同?以及为什么Timer在从Process运行时会被杀死,而在命令行运行脚本时不会被kill? – Ilya
你应该看看python的[GIL](http://www.dabeaz.com/python/UnderstandingGIL.pdf)。基本上在Python中,2个进程根本不互相通信。从命令行运行可以在单个进程中运行代码,并且所有变量(“Timer”)都是“共享的”。在另一个进程中运行会创建一个父进程(调用脚本)和一个子进程(使用'p = Process(...)'创建)。父进程不会与子进行通信,并且不知道另一个线程已启动。 – CoMartel