简体   繁体   English

尝试使用线程或多处理来使两个while循环同时运行

[英]Trying to get two while loops to run concurrently using threading or multiprocessing

According to examples online, these two methods I've tried should be the solution to my problem (see code). 根据在线示例,我尝试过的这两种方法应该可以解决我的问题(请参见代码)。 These two while loops are running one after the other even through they are in separate threads. 这两个while循环即使在单独的线程中也一个接一个地运行。

I've tried threading and multiprocessing. 我尝试了线程和多处理。

    global numberit
    numberit= 0
    global numberg
    numberg= 0
    def countingit(numberit):
        while numberit < 10:
            numberit += 1
            print("counter ", numberit)
            # time.sleep(1)

    def garbage(numberg):
        while numberg < 10:
            numberg += 1
            print("garbage ", numberg)
            # time.sleep(1)


    # threading.Thread(target=countingit(numberit)).start()
    # threading.Thread(target=garbage(numberg)).start()

    if  __name__ == '__main__':
        Process(target=countingit(numberit)).start()
        Process(target=garbage(numberg)).start()
    #     threading.Thread(target=countingit(numberit)).start()
    #     threading.Thread(target=garbage(numberg)).start()

I'm trying to get it to print: 我正在尝试打印它:

counter 1 garbage 1 counter 2 garbage 2 柜台1垃圾1柜台2垃圾2

... and so on. ... 等等。

The plan is to run while loop threads concurrently with a tkinter gui with push buttons. 该计划是在循环线程与带有按钮的tkinter gui同时运行时运行。 but i cant get them to run at the same time. 但是我不能让它们同时运行。 One process always has to complete before the other starts. 一个过程总是必须在另一个过程开始之前完成。

Thank you. 谢谢。

I've already tried what is shown in the example code I have provided. 我已经尝试了提供的示例代码中显示的内容。

Instead of having each while loop run in intervals, I get them running one after the other which is not the desired outcome. 我没有让每个while循环间隔运行,而是让它们一个接一个地运行,这不是期望的结果。 I'm trying this as a test to then add a tkinter gui in another thread. 我正在尝试将其作为测试,然后在另一个线程中添加tkinter gui。

This is the result: 结果如下:

counter 1 ... counter 10 garbage 1 ... garbage 10 柜台1 ...柜台10垃圾1 ...垃圾10

But would like: counter 1 garbage 1 ... counter 10 garbage 10 但想:计数器1垃圾1 ...计数器10垃圾10

I see a problem in these two lines: 我在这两行中看到一个问题:

 threading.Thread(target=countingit(numberit)).start()
 threading.Thread(target=garbage(numberg)).start()

This is a common antipattern -- instead of making a thread that calls countingit with the argument numberit , this code calls countingit right away in the main thread, and then passes the return value to the Thread initializer. 这是一种常见的反模式-而不是创建一个使用参数numberit调用countingit的线程,此代码立即在主线程中调用countingit ,然后将返回值传递给线程初始化程序。

To pass arguments to a function being called by a thread, use the args parameter. 要将参数传递给线程正在调用的函数,请使用args参数。 Make sure to pass it as a tuple, even if there's only one argument. 即使只有一个参数,也请确保将其作为一个元组传递。

threading.Thread(target = countingit, args=(numberit,)).start()
threading.Thread(target = garbage, args=(numberit,)).start()

When I run this on my machine, I get output that is interleaved as desired: 当我在机器上运行它时,我得到的输出是按需交错的:

counter  1
counter  2
garbage  1
counter  3
garbage  2
counter  4
counter  5
garbage  3
counter  6
counter  7
garbage  4
counter  8
garbage  5
counter  9
garbage  6
counter  10
garbage  7
garbage  8
garbage  9
garbage  10

(all of this advice applies to your Process-based attempt, as well) (所有这些建议也适用于您基于流程的尝试)

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

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