Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python using a thread in an infinite loop

How can I use threading in order to run 2 processes simultaneously infinitely? I have a chat program, and I want to input while having stuff print out.

I have done a bit of research into thread, and it seems really complicated. What I have so far is

t = Thread(target=refresh)    
t.daemon = True
t.start()

I have a refresh() function.

But I'm pretty sure this is wrong, and I have no idea how to run this infinitely alongside my input. Can someone explain how threading works, and how I can run it infinitely alongside another infinite loop?

like image 582
Kevin Avatar asked Oct 18 '25 18:10

Kevin


1 Answers

You have to start your thread like you did it in your code. The important thing is to wait for the thread to complete (even if it infinite loop) because without it, you program will stop immediatly. Just use

t.join()

And you code will run until t thread is over. If you want two thread, just do like this

t1.start()
t2.start()

t1.join()
t2.join()

And your two thread will run simultanously

For your crash problem, with multithreading, you have to look at Mutex for the IOerror

like image 121
F4T4liS Avatar answered Oct 20 '25 08:10

F4T4liS