简体   繁体   English

如何在python的线程内运行无限循环

[英]How to run infinite loops within threading in python

I've looked everywhere and it seems like the threads I write should work. 我到处都看过了, 似乎我编写的线程应该工作了。 I've checked many other threads about it and tutorials. 我已经检查了许多其他有关它的线程和教程。 I can't seem to run infinite loops in threads. 我似乎无法在线程中运行无限循环。 Despite what I do, only the first thread works/prints. 尽管我做了什么,但只有第一个线程可以工作/打印。

Here is the code for just methods. 这是方法的代码。

import threading


def thread1():
      while True:
            print("abc")
def thread2():
      while True:
            print ("123")

if __name__ == "__main__":
    t1 = threading.Thread(target=thread1())
    t2 = threading.Thread(target=thread2())

    t1.start
    t2.start

    t1.join
    t2.join

Removing the prentheses at the end of calling the functions with target= causes nothing to print so I keep that in there. 在使用target=调用函数结束时删除假义,不会打印任何内容,因此我将其保留在那里。

Here is the class/object version. 这是类/对象版本。

from threading import Thread
class Thread1(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start

    def run():
        while True:
            print ("abc")

class Thread2(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start

    def run():
        while True:
            print ("123")

Thread1.run()
Thread2.run()

both never get to printing 123 and I can't figure out why. 两者都从来没有打印123 ,我不知道为什么。 It looks like infinite loops shouldn't be an issue because they are being run in parallel. 看起来无限循环不应该成为问题,因为它们是并行运行的。 I tried time.sleep (bc maybe the GIL stopped it idk) so thread2 could run while thread1 was idle. 我尝试了time.sleep (可能是GIL停止了它的idk),所以线程2可以在线程1空闲时运行。 Didn't work. 没用

For the first example: 对于第一个示例:

if __name__ == "__main__":
    t1 = threading.Thread(target=thread1)
    t2 = threading.Thread(target=thread2)

    t1.start()
    t2.start()

    t1.join()
    t2.join()

Pass the function, not the result of calling the function, to threading.Thread as its target . 将函数而不是调用函数的结果传递给threading.Thread作为其target

For the section example. 对于本节示例。 Don't call run . 不要打电话给run Call start . 呼叫start After creating an instance. 创建实例后。

from threading import Thread
class Thread1(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True

    def run():
        while True:
            print ("abc")

class Thread2(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True

    def run():
        while True:
            print ("123")

Thread1().start()
Thread2().start()

Your parentheses are in the wrong places in the first version. 您的括号在第一个版本中的位置错误。

As currently written, you are passing the result of calling thread1 when creating t1 ; 如当前所写,您正在创建t1时传递调用thread1的结果; since that call never returns, you never actually create a thread. 由于该调用永远不会返回,因此您实际上不会创建线程。 So you need to remove those parentheses. 因此,您需要删除那些括号。

But you don't include the parentheses where you are trying to call the start and join methods, so the threads never actually got started even when you didn't use the extra parentheses when creating the threads. 但是您没有在试图调用startjoin方法的地方加上括号,因此即使您在创建线程时没有使用多余的括号,也不会真正启动线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM