Python 在类Init上每X秒执行一次函数
我已经创建了一个类,我想广播预定的消息。我对线程(在任何语言中)没有太多经验,但我很难掌握python的概念 我最近的尝试是使用如下内容:Python 在类Init上每X秒执行一次函数,python,multithreading,Python,Multithreading,我已经创建了一个类,我想广播预定的消息。我对线程(在任何语言中)没有太多经验,但我很难掌握python的概念 我最近的尝试是使用如下内容: class myBot(JabberBot): def __init__( self, jid, password, res = none): autoNotify() def autoNotify(): #Send timed message self.send('som
class myBot(JabberBot):
def __init__( self, jid, password, res = none):
autoNotify()
def autoNotify():
#Send timed message
self.send('someuser@jabber.example.com','cooool message text!')
#set/reset timer
t = Timer(05,self.autoNotify)
t.start()
import threading
import time
class myBot(JabberBot, threading.Thread):
def __init__( self, jid, password, res = none):
threading.Thread.__init__(self)
def run(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
myBot(...).start()
class myBot(JabberBot):
def start(self):
threading.Thread(target=self.autoNotifyLoop).start()
def autoNotifyLoop(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
问题是它会不断产生新的线程,直到最终死掉。我已经阅读了大量使用第三方库、消息队列和twisted的示例,但我的问题很简单——是否真的没有简单的方法生成一个异步线程
但是,您不应该在构造函数中生成线程。相反,提供一个run
方法并从threading.Thread
继承,这将使一个公共start
方法可用,可用于启动通知循环。大概是这样的:
class myBot(JabberBot):
def __init__( self, jid, password, res = none):
autoNotify()
def autoNotify():
#Send timed message
self.send('someuser@jabber.example.com','cooool message text!')
#set/reset timer
t = Timer(05,self.autoNotify)
t.start()
import threading
import time
class myBot(JabberBot, threading.Thread):
def __init__( self, jid, password, res = none):
threading.Thread.__init__(self)
def run(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
myBot(...).start()
class myBot(JabberBot):
def start(self):
threading.Thread(target=self.autoNotifyLoop).start()
def autoNotifyLoop(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
这样使用:
class myBot(JabberBot):
def __init__( self, jid, password, res = none):
autoNotify()
def autoNotify():
#Send timed message
self.send('someuser@jabber.example.com','cooool message text!')
#set/reset timer
t = Timer(05,self.autoNotify)
t.start()
import threading
import time
class myBot(JabberBot, threading.Thread):
def __init__( self, jid, password, res = none):
threading.Thread.__init__(self)
def run(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
myBot(...).start()
class myBot(JabberBot):
def start(self):
threading.Thread(target=self.autoNotifyLoop).start()
def autoNotifyLoop(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
如果出于某种原因不能或不想使用多重继承,也可以执行以下操作:
class myBot(JabberBot):
def __init__( self, jid, password, res = none):
autoNotify()
def autoNotify():
#Send timed message
self.send('someuser@jabber.example.com','cooool message text!')
#set/reset timer
t = Timer(05,self.autoNotify)
t.start()
import threading
import time
class myBot(JabberBot, threading.Thread):
def __init__( self, jid, password, res = none):
threading.Thread.__init__(self)
def run(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
myBot(...).start()
class myBot(JabberBot):
def start(self):
threading.Thread(target=self.autoNotifyLoop).start()
def autoNotifyLoop(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
您还可以为此创建一个函数,以获得最大的“便利”:
但是,您不应该在构造函数中生成线程。相反,提供一个run
方法并从threading.Thread
继承,这将使一个公共start
方法可用,可用于启动通知循环。大概是这样的:
class myBot(JabberBot):
def __init__( self, jid, password, res = none):
autoNotify()
def autoNotify():
#Send timed message
self.send('someuser@jabber.example.com','cooool message text!')
#set/reset timer
t = Timer(05,self.autoNotify)
t.start()
import threading
import time
class myBot(JabberBot, threading.Thread):
def __init__( self, jid, password, res = none):
threading.Thread.__init__(self)
def run(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
myBot(...).start()
class myBot(JabberBot):
def start(self):
threading.Thread(target=self.autoNotifyLoop).start()
def autoNotifyLoop(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
这样使用:
class myBot(JabberBot):
def __init__( self, jid, password, res = none):
autoNotify()
def autoNotify():
#Send timed message
self.send('someuser@jabber.example.com','cooool message text!')
#set/reset timer
t = Timer(05,self.autoNotify)
t.start()
import threading
import time
class myBot(JabberBot, threading.Thread):
def __init__( self, jid, password, res = none):
threading.Thread.__init__(self)
def run(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
myBot(...).start()
class myBot(JabberBot):
def start(self):
threading.Thread(target=self.autoNotifyLoop).start()
def autoNotifyLoop(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
如果出于某种原因不能或不想使用多重继承,也可以执行以下操作:
class myBot(JabberBot):
def __init__( self, jid, password, res = none):
autoNotify()
def autoNotify():
#Send timed message
self.send('someuser@jabber.example.com','cooool message text!')
#set/reset timer
t = Timer(05,self.autoNotify)
t.start()
import threading
import time
class myBot(JabberBot, threading.Thread):
def __init__( self, jid, password, res = none):
threading.Thread.__init__(self)
def run(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
myBot(...).start()
class myBot(JabberBot):
def start(self):
threading.Thread(target=self.autoNotifyLoop).start()
def autoNotifyLoop(self):
while True:
self.autoNotify()
time.sleep(5) # wait 4 seconds
def autoNotify(self):
self.send('someuser@jabber.example.com','cooool message text!')
您还可以为此创建一个函数,以获得最大的“便利”:
非常棒的例子,非常感谢!有没有可能进一步说明为什么不应该在构造函数中生成线程?我想我已经明白为什么了,但我想从比我更了解的人那里知道。@HurnsMobile:这是一条简单的规则:除了初始化,永远不要在构造函数中做任何事情。API的用户不希望构造函数有副作用,所以不要这样做。它已经并将导致难以追踪的细微缺陷。非常棒的例子,非常感谢!有没有可能进一步说明为什么不应该在构造函数中生成线程?我想我已经明白为什么了,但我想从比我更了解的人那里知道。@HurnsMobile:这是一条简单的规则:除了初始化,永远不要在构造函数中做任何事情。API的用户不希望构造函数有副作用,所以不要这样做。它已经并将导致难以追踪的细微缺陷。