[英]continuosly running function in background in python
我想和我的主进程并行运行一个函数continuoulsy。我如何在python中进行?多处理?线程或线程模块? 我是python的新手。非常感谢。
如果目标是捕获stderr
并执行某些操作,您只需用自定义对象替换sys.stderr
:
>>> import sys
>>> class MyLogger(object):
... def __init__(self, callback):
... self._callback = callback
... def write(self, text):
... if 'log' in text:
... self._callback(text)
... sys.__stderr__.write(text) # continue writing to normal stderr
...
>>> def the_callback(s):
... print('Stderr: %r' % s)
...
>>> sys.stderr = MyLogger(the_callback)
>>> sys.stderr.write('Some log message\n')
Stderr: 'Some log message'
Some log message
>>>
>>> sys.stderr.write('Another message\n')
Another message
如果要处理回溯和异常,可以使用sys.excepthook
。
如果要捕获日志logging
模块创建的logging
,可以实现类似于上述Logger
自己的Handler
类,但重新实现emit
方法。
一个更有趣但不太实际的解决方案是使用某种调度程序和生成器来模拟并行执行而不实际创建线程(在互联网上搜索会产生一些很好的结果)
这绝对取决于你的目标,但我建议看一下线程模块。 有关使用threading
和multithreading
许多好的StackOverflow问题(例如, Multiprocessing vs Threading Python )。
这是我的一个项目的简要骨架:
import threading # Threading module itself
import Queue # A handy way to pass tasks to your thread
job_queue = Queue.Queue()
job_queue.append('one job to do')
# This is the function that we want to keep running while our program does its thing
def function_to_run_in_background():
# Do something...here is one form of flow control
while True:
job_to_do = job_queue.get() # Get the task from the Queue
print job_to_do # Print what it was we fetched
job_queue.task_done() # Signal that we've finished with that queue item
# Launch the thread...
t = threadingThread(target=function_to_run_in_background, args=(args_to_pass,))
t.daemon = True # YOU MAY NOT WANT THIS: Only use this line if you want the program to exit without waiting for the thread to finish
t.start() # Starts the thread
t.setName('threadName') # Makes it easier to interact with the thread later
# Do other stuff
sleep(5)
print "I am still here..."
job_queue.append('Here is another job for the thread...')
# Wait for everything in job_queue to finish. Since the thread is a daemon, the program will now exit, killing the thread.
job_queue.join()
如果你只想在同一个进程中在后台运行一个函数,请执行以下操作:
import thread
def function(a):
pass
thread.start_new(function, (1,)) # a is 1 then
我发现客户端 - 服务器架构对我来说是解决方案。 运行服务器,并产生许多客户端直接与服务器和客户端通信,如信使。
可以通过位于内存中的网络或文本文件来实现通话/通信(以加快速度并节省硬盘驱动器)。
Bakuriu:给出一个关于记录模块的好建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.