How to not wait for function to finish python

To expand on blue_note, let's say you have a function with arguments:

def test(b):
    global a
    time.sleep(1)
    a += 1 + b

You need to pass in your args like this:

from threading import Thread
b = 1
Thread(target=test, args=(b, )).start()
print("this will be printed immediately")

Note args must be a tuple.


A simple way is to run test() in another thread

import threading

th = threading.Thread(target=test)
th.start()

You can put it in a thread. Instead of test()

from threading import Thread
Thread(target=test).start()
print("this will be printed immediately")

Tags:

Python