forked from digitalocean/sample-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
38 lines (28 loc) · 822 Bytes
/
server.py
File metadata and controls
38 lines (28 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import asyncio
from aiohttp import web
async def Task1():
for i in range(100):
print ('Task-1',i)
await asyncio.sleep(1)
async def handle(request):
name = request.match_info.get('name', "Anonymous")
text = "Hello, " + name
return web.Response(text=text)
async def HttspServer():
# create the application, as before
app = web.Application()
app.add_routes([
web.get('/', handle),
web.get('/{name}', handle)
])
# set up the web server
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner)
await site.start()
# wait forever, running both the web server and the tasks
await asyncio.Event().wait()
loop=asyncio.new_event_loop()
loop.create_task(Task1())
loop.create_task(HttspServer())
loop.run_forever()