对于以下应用程序要使用哪些python模块,你们有什么建议吗:我想创建一个运行两个线程的守护进程,两个线程都有while True:循环。
任何例子都将不胜感激!提前谢谢。
更新:
这是我想出来的,但行为不是我所期望的。import time
import threading
class AddDaemon(object):
def __init__(self):
self.stuff = 'hi there this is AddDaemon'
def add(self):
while True:
print self.stuff
time.sleep(5)
class RemoveDaemon(object):
def __init__(self):
self.stuff = 'hi this is RemoveDaemon'
def rem(self):
while True:
print self.stuff
time.sleep(1)
def run():
a = AddDaemon()
r = RemoveDaemon()
t1 = threading.Thread(target=r.rem())
t2 = threading.Thread(target=a.add())
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
t2.start()
while True:
pass
run()
输出Connected to pydev debugger (build 163.10154.50)
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
当我尝试使用以下方法创建线程对象时:t1 = threading.Thread(target=r.rem())
t2 = threading.Thread(target=a.add())
r.rem()中的while循环是唯一执行的循环。我做错什么了?