账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    python threads can only be started once
    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死掉了,就没办法监控了,该如何改我的代码呢?

    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • In Python, threads can be started and run concurrently using the threading module. However, it is important to note that threads can only be started once, as the threading module only provides a way to create and manage threads. When you create a new thread, you must assign it to a thread object using the threading.Thread() constructor. Once you have assigned a thread to a thread object, you can start it by calling the start() method on that object. This will start the thread and give it a chance to execute. It is important to note that the start() 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 the start() method is blocking.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部