使每个python函数异步/等待
aioif的Python项目详细描述
aioify
使每个函数异步并可等待。
用法
pip install aioify
例如,使os
、shutil
和用户定义的函数可等待。
#!/usr/bin/env python############ Warning ############# This code should be executed only on POSIX OS with at least 1 GiB free space in /tmp/ directory and RAM!fromaioifyimportaioifyimportosimportshutildefgenerate_big_file(filename,file_size):withopen(file=filename,mode='wb')asf:f.write(os.urandom(file_size))aiogenerate_big_file=aioify(obj=generate_big_file)aios=aioify(obj=os,name='aios')aioshutil=aioify(obj=shutil,name='aishutil')asyncdefmain():dir_path='/tmp/big-files/'awaitaios.makedirs(name=dir_path,exist_ok=True)filename=os.path.join(dir_path,'original')copy_filename=os.path.join(dir_path,'copy')file_size=1024*1024*1024awaitaiogenerate_big_file(filename=filename,file_size=file_size)awaitaioshutil.copy(src=filename,dst=copy_filename)awaitaioshutil.rmtree(path=dir_path)if__name__=='__main__':importasyncioasaioloop=aio.get_event_loop()loop.run_until_complete(main())