aurelius
aurelius

Reputation: 528

Threading Python3

I am trying to use Threading in Python, and struggle to kick off two functions at the same time, then wait for both to finish and load returned data into variables in the main code. How can this be achieved?

import threading
from threading import Thread

func1():
 #<do something>
 return(x,y,z)

func2():
 #<do something>
 return(a,b,c)

Thread(target=func1).start()
Thread(target=func2).start()
#<hold until both threads are done, load returned values>

Upvotes: 0

Views: 202

Answers (1)

redmonkey
redmonkey

Reputation: 86

More clarity is definitely required from the question asked. Perhaps you're after something like the below?

import threading
from threading import Thread

def func1():
 print("inside func1")
 return 5

def func2():
    print("inside func2")
    return 6


if __name__ == "__main__":
    t1 = Thread(target=func1)
    t2 = Thread(target=func2)
    threads = [t1, t2]
    for t in threads:
        t.start()

I believe you were missing the start() method to actually launch your threads?

Upvotes: 1

Related Questions