obj = functioning()
from threading import Thread
Thread(target=obj.runCron(cronDetails)).start()
print "new thread started..."
我正在运行它,这应该作为runCorn函数的新线程运行,并且应该打印新线程。但这不是打印新线程而不是创建新线程
答案 0 :(得分:2)
您的问题缺少一些细节,例如你得到了什么错误信息等等 - 下面是你的代码后模仿的工作示例。
#!/usr/bin/env python
import time
class Obj(object):
def runCron(self, cronDetails):
time.sleep(1)
print cronDetails
obj = Obj()
cronDetails = "I'm here."
from threading import Thread
# Note, that the `target` is a function object
# (or a callable in general), we don't actually call it yet!
t = Thread(target=obj.runCron, args=(cronDetails, ))
t.start()
print "New thread started (should be here in a second) ..."
打印:
New thread started (should be here in a second) ...
I'm here.
答案 1 :(得分:1)
看起来你想在线程中调用obj.runCron(cronDetails)
。但该代码的作用是先调用obj.runCron(cronDetails)
,然后将的结果传递给Thread
类。
如果是这种情况,下面的代码应该修复它:
obj = functioning()
from threading import Thread
Thread(target=obj.runCron, args=(cronDetails,)).start()
print "new thread started..."
请注意,我不再自己调用obj.runCron
,而是将带有参数的方法分别传递给threading.Thread
,因此可以在线程中使用正确的方法调用参数。
如果这不符合您的要求,请按照我在评论中提出的要求提供更多信息。