Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to run two while loops at the same time using threading? [closed]

I have looked through all the previous answers, and they are all too complicated for a beginner like me. I want to run too while loops at the same time. For example, I want to run these two at the same time:

def firstFunction():
    do things

def secondFunction():
    do some other things

As I said, the other answers are too complex for me to understand.

like image 420
Jacob Barrow Avatar asked Dec 14 '25 12:12

Jacob Barrow


1 Answers

Assuming your while loops are within the functions you listed, this is the easiest way I can think of.

from threading import Thread

t1 = Thread(target = firstFunction)
t2 = Thread(target = secondFunction)

t1.start()
t2.start()

As pointed out by tdelaney, doing it this way will just kick off each thread and immediately move on. If you need to wait for those threads to complete before running the rest of your program you can use the .join() method.

like image 121
James Robinson Avatar answered Dec 17 '25 02:12

James Robinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!