擅长:python、mysql、java
<p>您的<code>RenderThread</code>类似乎不完整(缺少<code>:</code>和<code>Thread</code>初始化)。
一旦完成,您的示例似乎会按预期工作</p>
<p><strong>编辑</strong>问题中的示例更改后,仍然有效</p>
<pre><code>#!/usr/bin/env python
import time
import threading
class ReaderThread(threading.Thread):
def __init__(self, data):
super().__init__()
self.data = data
self.exit = False
def run(self):
t0=time.time()
while not self.exit:
# somehow modify self.data
time.sleep(0.25)
self.data.add(int(100.0*(time.time()-t0)))
def stop(self):
self.exit = True
self.join()
class MainThread():
def __init__(self):
self.data = set()
self.thread = ReaderThread(self.data)
def function(self):
self.thread.start()
time.sleep(2) # whoptydoo
self.thread.stop()
mt=MainThread()
mt.function()
print(mt.data) # {225, 100, 200, 75, 175, 50, 150, 25, 125}
</code></pre>