我想在我的主进程中并行运行一个函数。我如何在 python 中执行它?多处理?线程或线程模块?我是python的新手。非常感谢任何帮助。
问问题
7943 次
4 回答
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
方法。
一个更有趣但不太实用的解决方案是使用某种调度程序和生成器来模拟并行执行而不实际创建线程(在互联网上搜索会产生一些不错的结果)
于 2012-12-18T14:00:16.447 回答
0
这绝对取决于您的目标,但我建议您查看线程模块。threading
关于and的使用有很多很好的 StackOverflow 问题multithreading
(例如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()
于 2012-12-18T13:22:21.983 回答
0
我发现客户端-服务器架构是我的解决方案。运行服务器,并产生许多客户端直接与服务器和客户端之间进行对话,例如 messenger。
可以通过网络或内存中的文本文件进行通话/通信(以加快速度并节省硬盘驱动器)。
Bakuriu:给你关于日志模块的好建议。
于 2012-12-18T16:10:17.760 回答
0
如果您只想在同一进程中在后台运行一个函数,请执行以下操作:
import thread
def function(a):
pass
thread.start_new(function, (1,)) # a is 1 then
于 2012-12-18T13:24:55.497 回答