提供简单的线程接口,以便轻松执行异步操作
py_easy_async的Python项目详细描述
py_easy_async提供简单的线程接口,以便在python 3.4及更高版本中轻松执行异步操作
简单异步操作示例
importpy_easy_asyncimporttimedefprint_with_sleep(word):time.sleep(1)print(word)return'done'print('first')py_easy_async.async(lambda:print_with_sleep('second'),lambdadone_string:print("done callback called with string: %s"%done_string))print('third')
上面的示例将打印以下内容:
first third second done callback called with string: done
线程池管理示例
frompy_easy_asyncimportpooldefmessage_handler(message):print('got message:',message)identifier=pool.run_thread(message_handler,name='Persistent-Thread')foriinrange(5):pool.message(identifier,"test message #%s"%i)pool.stop_thread(identifier)
上面的示例将启动线程,该线程将等待它应该处理的消息:
Persistent-Thread starting... got message: test message #0 got message: test message #1 got message: test message #2 got message: test message #3 got message: test message #4 Persistent-Thread exiting...
许可证
在麻省理工学院的许可下发布
安装
pip install py_easy_async