2017-09-19 7 views
0

私はRequests_Threadsライブラリを試しています。私はそれが私の望むやり方でデータを返すところに持っていますが、それは私にProcess finished with exit code 0を与えて、前に出ています。誰でもこのライブラリを使用し、それを理解しましたか?Requests_ThreadsとAsyncIOで実行した後で終了する

from requests_threads import AsyncSession 
import asyncio 

class apis: 
    def __init__(self): 
     self.session=AsyncSession() 
     self.main_out=self.session.run(self.main) 
     print('still alive') 

    async def main(self): 
     rs = [] 
     for _ in range(100): 
      rs.append(await self.sub('thing')) 

     return[ (x[0].json(),x[1]) for x in rs] 

    async def sub(self,key): 
     return await self.session.get('http://httpbin.org/get'),key 

答えて

0

だから、私は

1000 aiohttp使用したソリューションになってしまった標準の要求を使用して取得することが56秒、または54%強の削減を取っaiohttp使用して、2分4秒かかりました。

import aiohttp 
import asyncio 
from datetime import datetime 

class apis: 
    def __init__(self): 
     self.session=aiohttp.ClientSession() 
     self.loop=asyncio.get_event_loop() 
     self.main_out=self.loop.run_until_complete(self.main()) 

    async def main(self): 
     rs = [] 
     for _ in range(1000): 
      rs.append(await self.loop.create_task(self.sub('foo'))) 
     return rs 

    async def sub(self,key): 
     return await self.session.get('http://httpbin.org/get'),key,datetime.now() 
関連する問題