-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
48 lines (37 loc) · 1023 Bytes
/
service.py
File metadata and controls
48 lines (37 loc) · 1023 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
39
40
41
42
43
44
45
46
47
48
import os
import sys
if sys.implementation.name == 'micropython':
import uasyncio as asyncio
else:
import asyncio
import configuration
class Service:
async def onStart(self):
pass
async def _run(self, runner):
def onStop():
runner._doRun = False
runner._stop(self)
while runner._doRun:
await self.onLoop(onStop)
return await self.onStop()
async def onStop(self):
pass
class Runner:
def __init__(self):
self._services = []
self._doRun = True
self._stopService = None
def add(self, service):
self._services.append(service)
return self
async def run(self):
await asyncio.gather(
*[s.onStart() for s in self._services]
)
results = await asyncio.gather(
*[s._run(self) for s in self._services],
)
return self._stopService, results
def _stop(self, service):
self._stopService = service