How to not wait for function to finish python

后端 未结 5 885
青春惊慌失措
青春惊慌失措 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:19

    You should look at a library meant for asynchronous requests, such as gevent

    Examples here: http://sdiehl.github.io/gevent-tutorial/#synchronous-asynchronous-execution

    import gevent
    
    def foo():
        print('Running in foo')
        gevent.sleep(0)
        print('Explicit context switch to foo again')
    
    def bar():
        print('Explicit context to bar')
        gevent.sleep(0)
        print('Implicit context switch back to bar')
    
    gevent.joinall([
        gevent.spawn(foo),
        gevent.spawn(bar),
    ])
    

提交回复
热议问题