python中 while true_Python线程:多个While True循环

对于以下应用程序要使用哪些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循环是唯一执行的循环。我做错什么了?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值