Python,使用多处理线程

时间:2016-09-19 12:14:54

标签: python multithreading python-multiprocessing

有人可以解释为什么线程不能在多处理中工作。过程。

我附上了一些例子来解释我的问题。

我有一个每秒执行一次并写入文件的进程。当我从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只执行一次,但是如果我使用Thread(target = collect_statistics).start()就好像我从shell运行它一样。为什么会这样?

1 个答案:

答案 0 :(得分:1)

以下是发生的事情:

  1. 您开始了您的流程
  2. collect_statistics运行
  3. 计时器已启动
  4. 现在进程中调用的函数(collect_statistics)已完成,因此该过程 退出,同时杀死计时器。
  5. 以下是修复方法:

    <强> 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()就是为了取代你无限的循环,这需要大量的资源。

相关问题