我有一个jython类,它作为一个线程运行。 我希望它的运行方法创建一个java计时器,然后调度我的类的一个函数:
class IBTHXHandler(threading.Thread):
def __init__(self):
threading.Thread.__init__(self, name='IBTHX Handler Thread')
self.start()
def run(self):
print 'ibthx thread running'
timer = Timer
timer.schedule(self.getRealtimeData(), 0, 1000)
def getRealtimeData(self):
print 'Getting Realtime Data'
当我运行此代码时,出现此错误:
TypeError: schedule(): 1st arg can't be coerced to java.util.TimerTask
我也试过
timer.schedule(self.getRealtimeData, 0, 1000)
这给了我
TypeError: schedule(): self arg can't be coerced to java.util.Timer
有没有比使用 Java 计时器更好的方法来解决这个问题?
我看了使用 python 线程。计时器类,但这给了我问题(我想是因为我从另一个线程中调用它??
无论如何,感谢您查看此内容。
代码有两个问题。第一个是你忘记了 Timer 后面的 () 来实例化它,第二个是要调度的第一个参数必须是计时器任务。以下代码应该有效。希望这有帮助!
import threading
from java.util import Timer, TimerTask
class MyTimerTask(TimerTask):
def run(self):
print 'Getting Realtime Data'
class IBTHXHandler(threading.Thread):
def __init__(self):
threading.Thread.__init__(self, name='IBTHX Handler Thread')
self.start()
def run(self):
print 'ibthx thread running'
timer = Timer()
timer.schedule(MyTimerTask(), 0, 1000)
IBTHXHandler()