擅长:python、mysql、java
<p>只是继承自线程。线程并且使用这个类而不是Thread-只要你能控制线程。在</p>
<pre><code>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()
</code></pre>
<p>就像一幅粗糙的素描。在</p>
<p><strong>编辑</strong>
从现有的应用程序中注入新的“评论”的需要。假设您有一个脚本,它本身导入其他库。这些库使用<code>threading</code>模块:</p>
<p>在导入任何其他模块之前,首先执行此操作</p>
^{pr2}$
<p>由于后续导入只在<code>sys.modules</code>中进行查找,所有其他使用此功能的库现在都应该使用我们的新类。我认为这是一次黑客攻击,可能会有奇怪的副作用。但至少值得一试。在</p>
<p>请注意:<code>threading.Thread</code>不是在python中实现并发的唯一方法,还有其他选项,如<code>multiprocessing</code>等。。这些在这里不受影响。在</p>
<p><strong>编辑2</strong>
我刚刚看了一下你引用的库,它是关于进程的,而不是线程!所以,只要做一个<code>:%s/threading/multiprocessing/g</code>和<code>:%s/Thread/Process/g</code>就可以了。在</p>