简体   繁体   中英

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.

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

... and so on.

The plan is to run while loop threads concurrently with a tkinter gui with push buttons. 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. I'm trying this as a test to then add a tkinter gui in another thread.

This is the result:

counter 1 ... counter 10 garbage 1 ... garbage 10

But would like: counter 1 garbage 1 ... counter 10 garbage 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.

To pass arguments to a function being called by a thread, use the args parameter. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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