How to call the same function after n seconds?

The server is written on aiohttp web.

The server is the intermediary between the database and mobile applications. You need to add the update to the server every n seconds came to a certain web page, and under certain conditions called another function. However, he must continue to accept requests from mobile.

I implemented this idea in the following way:

from aiohttp import web
import json
import aiopg
import logging
import os



async def handle(request):
 name = request.match_info.get('name', "Anonymous")
 text = "Hello," + name
 return web.Response(text=text)

async def function2():
 #work with dB
pass

async def checkfile():
 # download and check the file
 if #condition:
 await function2()
asyncio.sleep(5)
checkfile()


server = web.Application()
server.add_routes([web.get('/', handle)#other routes and options])

await checkfile()

if __name__ == '__main__':
 web.run_app(server,port=os.environ['PORT'])


Or I came up with the wrong solution to the problem? Please help me to implement the task. Thank you in advance everyone!
April 3rd 20 at 18:43
2 answers
April 3rd 20 at 18:45
Solution
In the simplest case:
1. change recursion to while loop
2. correctly still await asyncio.sleep(5)
3. start loop.create_task(checkfile())
PS you can also look for ready libraries for this.
the while condition? I need to treat every page every n seconds, as it is updated a couple times a day, and time of updates is unknown. There is more a question of waiting for those n seconds - Gwendolyn commented on April 3rd 20 at 18:48
while n does not change - Gwendolyn commented on April 3rd 20 at 18:51
and how to call loop.create_task(checkfile()) in aiohttp? if the loop itself I'm not creating. Sory for stupid questions - Gwendolyn commented on April 3rd 20 at 18:54
@Gwendolyn, Yes you can the exact same while True, a sleep(n) and will have to wait for those n seconds - florencio.Torphy commented on April 3rd 20 at 18:57
@florencio.Torphy, can still, please paint loop.create_task(checkfile())? - Gwendolyn commented on April 3rd 20 at 19:00
@Gwendolyn, https://docs.aiohttp.org/en/stable/web_reference.h... - florencio.Torphy commented on April 3rd 20 at 19:03
@florencio.Torphy, added a function:
async def on_startup(app):
app.loop.create_task(checkfile())
pass

but it is not invoked.
on the Stak found that you need to add:
app.on_startup.append(on_startup)

how to make aiohttp cause on_startup(app)? - Gwendolyn commented on April 3rd 20 at 19:06
@Gwendolyn, well, it is correctly written. Let's you already own will read docks and look for examples if something is not clear. - florencio.Torphy commented on April 3rd 20 at 19:09
@florencio.Torphy, got it, thanks! - Gwendolyn commented on April 3rd 20 at 19:12
April 3rd 20 at 18:47
If someone will be useful - the solution to the problem, given the response @Joel.Torphy
from aiohttp import web
import json
import asyncio
import logging
import os



async def handle(request):
 name = request.match_info.get('name', "Anonymous")
 text = "Hello," + name
 return web.Response(text=text)



async def checkfile():
 while True:
 print("ho! ho! ho!")
 await asyncio.sleep(5)




async def on_startup(app):
app.loop.create_task(checkfile())
pass


server = web.Application()
server.on_startup.append(on_startup)
server.add_routes([web.get('/', handle)])



if __name__ == '__main__':
 web.run_app(server,port=8888)

Find more questions by tags PythonAIOHTTP