我想和我的主进程并行运行一个函数continuoulsy。我如何在python中进行?多处理?线程或线程模块? 我是python的新手。非常感谢。
答案 0 :(得分:2)
如果目标是捕获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
模块创建的日志,您可以实现类似于上述Handler
的自己的Logger
类,但重新实现emit
方法。
一个更有趣但不太实际的解决方案是使用某种调度程序和生成器来模拟并行执行而不实际创建线程(在互联网上搜索会产生一些很好的结果)
答案 1 :(得分:0)
这绝对取决于你的目标,但我建议看一下threading模块。关于threading
和multithreading
(例如Multiprocessing vs Threading Python)的使用,有许多好的StackOverflow问题。
以下是我的一个项目的简要骨架:
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()
答案 2 :(得分:0)
如果你只想在同一个过程中在后台运行一个函数,请执行:
import thread
def function(a):
pass
thread.start_new(function, (1,)) # a is 1 then
答案 3 :(得分:0)
我发现客户端 - 服务器架构对我来说是解决方案。运行服务器,并产生许多客户端直接与服务器和客户端通信,如messenger。
通过位于内存中的网络或文本文件可以实现通话/通信(为了加快速度并节省硬盘)。
Bakuriu:给你一个关于记录模块的好建议。