How to not wait for function to finish python

后端 未结 5 920
青春惊慌失措
青春惊慌失措 2021-02-19 11:42

I\'m trying to program a loop with a asynchronous part in it. I dont want to wait for this asynchronous part every iteration though. Is there a way to not wait for this function

5条回答
  •  情歌与酒
    2021-02-19 12:02

    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.

提交回复
热议问题