基于线程和多处理的异步脚本
aioscript的Python项目详细描述
info: | Base asyncio script with support of threading and multiprocessing |
---|
安装
pip install aioscript
用法
fromaiohttpimportClientSession,webfromaioscriptimportAbstractScriptclassScript(AbstractScript):defsetup(self):self.session=ClientSession(loop=self.loop)asyncdefclose(self):awaitself.session.close()asyncdefhandle(self,url):asyncwithself.session.get(url)asresponse:ifresponse.status==web.HTTPOk.status_code:print(response.url,'Ok')else:print(response.url,'Not ok')asyncdefpopulate(self):urls=['https://www.python.org/','https://www.python.org/doc/','https://docs.python.org/3/','https://docs.python.org/3/library/concurrency.html','https://docs.python.org/3/library/asyncio.html','https://docs.python.org/3/library/asyncio-eventloop.html',]forurlinurls:yieldurlif__name__=='__main__':Script().run()
python script.py --coroutines=10
python 3.6+是必需的