asynchronous programming in python

前端 未结 6 1793
醉梦人生
醉梦人生 2020-11-29 18:21

Is there a generic notion of asynchronous programming in python? Could I assign a callback to a function, execute it and return to the main program flow immediately, no matt

6条回答
  •  萌比男神i
    2020-11-29 19:18

    You may see my Python Asynchronous Programming tool: http://www.ideawu.com/blog/2010/08/delegate-in-pythonpython-asynchronous-programming.html

    import time, random, sys
    from delegate import *
    
    def proc(a):
        time.sleep(random.random())
        return str(a)
    
    def proc_callback(handle, args=None):
        ret = d.end(handle)
    
    d = Delegate()
    d.init(2) # number of workers
    
    handle = d.begin(proc, '12345', proc_callback, 'test')
    sys.stdin.readline()
    
    d.free()
    

提交回复
热议问题