python线程在后端运行进程

时间:2015-02-17 03:35:44

标签: python multithreading

我试图从一些数据源制作一些文本报告文件,这需要花费大量时间并模拟这个我编写了以下代码

  

我计划使用线程和思想t.daemon = True来做   解决目的,但程序在操作之前不会退出   完整

import random
import threading
import time
import logging

logging.basicConfig(level=logging.DEBUG,
                    format='(%(threadName)-10s) %(message)s',
                    )


def worker():
    """thread worker function"""
    t = threading.currentThread()
    tag = random.randint(1, 64)
    file_name = "/tmp/t-%d.txt" % (tag)
    logging.debug('started writing file - %s', file_name)
    f = open(file_name, 'w')
    for x in xrange(2 ** tag):  # total no of lines is 2**tag
        f.write("%d\n" % x)
    logging.debug('ending')
    f.close()
    return

# to simulate 5 files
for i in range(5):
    t = threading.Thread(target=worker)
    t.daemon = True
    t.start()

main_thread = threading.currentThread()
for t in threading.enumerate():
    if t is main_thread:
        continue
    logging.debug('joining %s', t.getName())
    t.join()
  

当我删除t.join()时,一些数据写入程序退出程序   退出很快,但添加t.join()使程序运行直到结束。有没有办法退出计划但是   进程应该仍在运行以完成后端任务。

1 个答案:

答案 0 :(得分:0)

您不是在寻找守护进程。事实上,你想确保你的进程不是一个守护进程,因为一旦剩下的只剩下你的程序就会被杀死。您正在寻找分离您的主题。

注意:如果我忘记杀死进程(因此它不会占用我的整个磁盘),最大降低到28。如果您希望它们停止,您将需要单独杀死每个进程!即“杀死13345”,如果您有“退出主13345”的消息(该线程超过2 ** 25)

另请注意:线程加入将持续到结束,因为您的程序未运行并且正在等待加入线程。

这是你想要的:

import logging 
import random
import multiprocessing
import time
import sys


#Make sure you don't write to stdout after this program stopped running and sub-processes are logging!
logging.basicConfig(level=logging.DEBUG,
                    format='(%(threadName)-10s) %(message)s',
                    )

def detach():
    p = multiprocessing.current_process()
    name = "worker" + str(p.pid)
    cc = multiprocessing.Process(name=name, target=worker)
    cc.daemon = False
    cc.start()
    logging.debug('Detached process: %s %s', p.name, p.pid)
    sys.stdout.flush()

def worker():
    """thread worker function"""
    #Should probably make sure there isn't already a thread processing this file already...
    tag = random.randint(5, 33) #Stop at 33 to make sure we don't take over the harddrive (8GB)
    file_name = "/tmp/t-%d.txt" % (tag)
    if tag > 26:
      logging.warning('\n\nThe detached process resulting from this may need to be killed by hand.\n')
    logging.debug('started writing file - %s', file_name)
    #Changed your code to use "with", available in any recent python version
    with open(file_name, 'w') as f:
        for x in xrange(2 ** tag):  # total no of lines is 2**tag
            f.write("%d\n" % x)
    return
#Stackoverflow: Keep scrolling to see more code!

# to simulate 5 files
for i in range(5):
    t = multiprocessing.Process(target=detach)
    t.daemon = False
    t.start()
    time.sleep(0.5)
    t.terminate()
    logging.debug("Terminating main program")