-1

okay so i'm trying to make a speedtest in python and the only problem i am having is that i cant figure out how to make the download speed threads finish before starting the upload speed threads. i can provide some of my code:

t1 = Thread(target= dload)
t2 = Thread(target= testo)
t3 = Thread(target= uload)
t4 = Thread(target= testo2)

t2.daemon=True
t4.daemon=True

t1.start()
t2.start()
t3.start()
t4.start()

i need t1 and t2 to end first, and then t3 and t4 should start. t2 is a daemon here because i want it to end as soon as t1 is done. forgot to mention that beforehand. i am completely new to threading so if this is really wrong please forgive me.

Undergod
  • 23
  • 2
  • 3
    For example `t1.join()` will wait until `t1` is done. – Klaus D. Mar 21 '22 at 09:35
  • Does this answer your question? [What is the use of join() in Python threading?](https://stackoverflow.com/questions/15085348/what-is-the-use-of-join-in-python-threading) – Klaus D. Mar 21 '22 at 09:43
  • well i tried to use join but i dont dont know how that would help. since i need the 4 threads to activate in pairs. is there a way to make that happen using join? – Undergod Mar 21 '22 at 09:48

2 Answers2

1

You use join for that. Note that t2.daemon=True means that you expect t2 to not end while the script is running. This is in opposition to your specification that you want to wait for t2 to end.

t1 = Thread(target= dload)
t2 = Thread(target= testo)
t3 = Thread(target= uload)
t4 = Thread(target= testo2)

# t2.daemon=True
t4.daemon=True

t1.start()
t2.start()

# wait for t1 and t2 to end
t1.join()
t2.join()

t3.start()
t4.start()
wackazong
  • 474
  • 7
  • 18
  • t2 is a daemon here because i dont want t2 to end until t1 ends. i might have given too little info in my question. sorry for that! – Undergod Mar 21 '22 at 09:55
  • 1
    @Undergod, That's not what "daemon" means. A daemon thread is a thread that the system will automatically kill (with no warning!) as soon as the last non-daemon thread in the program terminates. – Solomon Slow Mar 21 '22 at 14:39
0

There are 2 ways to solve your problem:

  • Using .join method. (Effective and clean)
  • Using functions (Not as effective than .join)
  1. Using .join function : see this answer.
  2. Using function:
    • The basic idea is threading one function and just calling the other function.
    • And if you want to run testo until t1 ends, then you can watch a variable and run the inside while loop. for example:
while t1_ends==False: # Change this value to true in t1 fucntion at the end of it.
    testo() 
t1 = Thread(target= dload)
t2 = Thread(target= testo)
t3 = Thread(target= uload)
t4 = Thread(target= testo2)

t4.daemon=True
def a():
    t1.start()
    testo()
def b():
    t3.start()
    t4.start()

a()
b()
Faraaz Kurawle
  • 1,085
  • 6
  • 24