- 120
- 0
我的代码:
import threading
import time
def test1():
while True:
print '111111111111'
time.sleep(2)
def test2():
while True:
print '22222222222'
time.sleep(10)
print '222222222222end'
break
return
def test3():
#获取所有线程
thread_list_all = threading.enumerate()
while True:
thread_list1 = threading.enumerate()
thread_list_number = len(thread_list1)
#如果有线程死掉
if thread_list_number < 4:
#查看哪个线程死掉
dead_thread = list(set(thread_list_all).difference(set(thread_list1)))
for item_thread in dead_thread:
print item_thread.getName(),'666666666666'
#重新启动线程
item_thread.start()
# item_thread.join()
time.sleep(1)
t1 = threading.Thread(target=test1,name=test1)
t2 = threading.Thread(target=test2,name=test2)
t3 = threading.Thread(target=test3,name=test3)
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
运行报错:
111111111111
22222222222
111111111111
111111111111
111111111111
111111111111
222222222222end
111111111111
<function test2 at 0x7f8df4c180c8> 666666666666
Exception in thread <function test3 at 0x7f8df4c18140>:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/freedom/work/app/sem/testvivo.py", line 28, in test3
item_thread.start()
File "/usr/lib/python2.7/threading.py", line 730, in start
raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
请问我如何才能监控我的线程呢?如果某个线程死掉了,就让他再重新启动。我上面的代码,如果线程3死掉了,就没办法监控了,该如何改我的代码呢?
- 共 0 条
- 全部回答
-
﹏嚼着口香糖的小女孩 普通会员 1楼
In Python, threads can be started and run concurrently using the
threadingmodule. However, it is important to note that threads can only be started once, as thethreadingmodule only provides a way to create and manage threads. When you create a new thread, you must assign it to a thread object using thethreading.Thread()constructor. Once you have assigned a thread to a thread object, you can start it by calling thestart()method on that object. This will start the thread and give it a chance to execute. It is important to note that thestart()method is blocking, which means that it will not return until the thread has finished executing. This can be useful in situations where you want a thread to execute a blocking operation, such as waiting for a database connection or reading a file. In summary, while threads can be started and run concurrently in Python, they can only be started once, and thestart()method is blocking.
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
