1
from aiohttp import web
from aiohttp import ClientSession
# this would go in a different file but keep it simple for now
class Generate:
# Get a person object from my website
async def get_person(self):
async with ClientSession() as session:
async with session.get('http://surveycodebot.com/person/generate') as response:
resp = await response.json()
# this prints the person
print(resp)
return resp
# loops `get_person` to get more than 1 person
async def get_people(self):
# array for gathering all responses
for _ in range(0,10):
resp = await self.get_person()
return resp
# class to handle '/'
class HomePage(web.View):
async def get(self):
# initiate the Generate class and call get_people
await Generate().get_people()
return web.Response(text="Hello, world")
if __name__ == "__main__":
app = web.Application()
app.router.add_get('/', HomePage)
web.run_app(app)
コードが正常に動作しています。 HomePage
の読み込みに時間がかかり、なぜ私は思っていました。私は28行目の利回りを使うべきだと思っていますが、私がそれを行うときには限界があります。ありがとう。Pythonの3 aiohttp |サーバーの応答時間を最適化する
は、私はそれがより高速に動作し、別のサーバーへのURLを変更した場合のように見えます。コードは、Get要求が行われた場所からサーバーコードを最適化する必要があるように見えます。すなわち変更 'ます。http:// surveycodebot.com /人/' HTTPSへgenerate':// jsonplaceholder.typicode.com/posts'ここでも今私に知らせて最適化する方法があります。 –