2016-09-19 135 views
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运行一样。为什么会发生?

回答

1

这里是正在发生的事情:

  1. 你开始你的过程
  2. collect_statistics运行
  3. 计时器开始
  4. 现在所谓的过程中(collect_statistics)的函数执行完毕,所以进程 退出,同时杀死定时器。

这里是如何解决它:

stat_collect.py

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

from threading import Timer 
from os import path 
from datetime import datetime 
import time 

STAT_DATETIME_FMT = '%Y-%m-%d %H:%M:%S' 


def collect_statistics(): 
    while True: 
     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') 

     time.sleep(1) 


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() 
    p.join() # wait until process is over, e.g forever 

p.join()只是我更换你无限而循环,这是没有花费很多的资源。

+0

那么,调用Process与从命令行运行stat_collect.py不同?以及为什么Timer在从Process运行时会被杀死,而在命令行运行脚本时不会被kill? – Ilya

+0

你应该看看python的[GIL](http://www.dabeaz.com/python/UnderstandingGIL.pdf)。基本上在Python中,2个进程根本不互相通信。从命令行运行可以在单个进程中运行代码,并且所有变量(“Timer”)都是“共享的”。在另一个进程中运行会创建一个父进程(调用脚本)和一个子进程(使用'p = Process(...)'创建)。父进程不会与子进行通信,并且不知道另一个线程已启动。 – CoMartel