Python中的线程起始钩子
在Python(2.7)中启动新线程时,是否有一种方法可以运行任意方法?我的目标是使用为每个生成的线程设置一个适当的标题。只要继承threading.thread并使用这个类而不是thread,只要您可以控制线程Python中的线程起始钩子,python,multithreading,Python,Multithreading,在Python(2.7)中启动新线程时,是否有一种方法可以运行任意方法?我的目标是使用为每个生成的线程设置一个适当的标题。只要继承threading.thread并使用这个类而不是thread,只要您可以控制线程 import threading class MyThread(threading.Thread): def __init__(self, callable, *args, **kwargs): super(MyThread, self).__init__(*
import threading
class MyThread(threading.Thread):
def __init__(self, callable, *args, **kwargs):
super(MyThread, self).__init__(*args, **kwargs)
self._call_on_start = callable
def start(self):
self._call_on_start()
super(MyThread, self).start()
就像一个粗略的草图
编辑
从这些注释中,需要将新的行为“注入”到现有的应用程序中。假设您有一个脚本,它本身导入其他库。这些库使用threading
模块:
在导入任何其他模块之前,首先执行此操作
import threading
import time
class MyThread(threading.Thread):
_call_on_start = None
def __init__(self, callable_ = None, *args, **kwargs):
super(MyThread, self).__init__(*args, **kwargs)
if callable_ is not None:
self._call_on_start = callable_
def start(self):
if self._call_on_start is not None:
self._call_on_start
super(MyThread, self).start()
def set_thread_title():
print "Set thread title"
MyThread._call_on_start = set_thread_title()
threading.Thread = MyThread
def calculate_something():
time.sleep(5)
print sum(range(1000))
t = threading.Thread(target = calculate_something)
t.start()
time.sleep(2)
t.join()
由于后续的导入只在sys.modules
中进行查找,因此所有使用该类的其他库现在都应该使用我们的新类。我认为这是一个黑客,它可能有奇怪的副作用。但至少值得一试
请注意:threading.Thread
不是python中实现并发的唯一方法,还有其他选项,如多处理
等。。这些在这里不会受到影响
编辑2
我只是看了一下你引用的库,它都是关于进程的,而不是关于线程的!所以,只要做一个
:%s/threading/multiprocessing/g
和:%s/Thread/Process/g
,事情就会好起来。只要继承threading.Thread并使用这个类而不是Thread,只要你能控制线程
import threading
class MyThread(threading.Thread):
def __init__(self, callable, *args, **kwargs):
super(MyThread, self).__init__(*args, **kwargs)
self._call_on_start = callable
def start(self):
self._call_on_start()
super(MyThread, self).start()
就像一个粗略的草图
编辑
从这些注释中,需要将新的行为“注入”到现有的应用程序中。假设您有一个脚本,它本身导入其他库。这些库使用threading
模块:
在导入任何其他模块之前,首先执行此操作
import threading
import time
class MyThread(threading.Thread):
_call_on_start = None
def __init__(self, callable_ = None, *args, **kwargs):
super(MyThread, self).__init__(*args, **kwargs)
if callable_ is not None:
self._call_on_start = callable_
def start(self):
if self._call_on_start is not None:
self._call_on_start
super(MyThread, self).start()
def set_thread_title():
print "Set thread title"
MyThread._call_on_start = set_thread_title()
threading.Thread = MyThread
def calculate_something():
time.sleep(5)
print sum(range(1000))
t = threading.Thread(target = calculate_something)
t.start()
time.sleep(2)
t.join()
由于后续的导入只在sys.modules
中进行查找,因此所有使用该类的其他库现在都应该使用我们的新类。我认为这是一个黑客,它可能有奇怪的副作用。但至少值得一试
请注意:threading.Thread
不是python中实现并发的唯一方法,还有其他选项,如多处理
等。。这些在这里不会受到影响
编辑2
我只是看了一下你引用的库,它都是关于进程的,而不是关于线程的!所以,只要做一个
:%s/threading/multiprocessing/g
和:%s/Thread/Process/g
,事情就会好起来。使用threading.setprofile
。您给它您的回调,Python将在每次新线程启动时调用它
文档。使用
threading.setprofile
。您给它您的回调,Python将在每次新线程启动时调用它
文档。这在理论上是可行的。然而,这是一个有点复杂的软件,多个组件/库创建线程,所以我真的不想这样做。这使得它非常困难。也许你可以做些肮脏的事。我不知道它是否有效,但我会添加一个例子。这在理论上是可行的。然而,这是一个有点复杂的软件,多个组件/库创建线程,所以我真的不想这样做。这使得它非常困难。也许你可以做些肮脏的事。不知道它是否有效,但我将添加一个示例。