3
0
Fork 0
mirror of https://github.com/YosysHQ/sby.git synced 2025-09-04 18:47:43 +00:00

Add asynchronous timer to detect timeouts.

Signed-off-by: William D. Jones <thor0505@comcast.net>
This commit is contained in:
William D. Jones 2019-03-29 02:03:45 -04:00 committed by Ed Bordin
parent 118280191a
commit b5350a2eb9

View file

@ -21,6 +21,7 @@ if os.name == "posix":
import resource, fcntl import resource, fcntl
import subprocess import subprocess
import asyncio import asyncio
from functools import partial
from shutil import copyfile, rmtree from shutil import copyfile, rmtree
from select import select from select import select
from time import time, localtime, sleep from time import time, localtime, sleep
@ -343,7 +344,26 @@ class SbyJob:
poll_fut = asyncio.ensure_future(self.task_poller()) poll_fut = asyncio.ensure_future(self.task_poller())
loop.run_until_complete(poll_fut) loop.run_until_complete(poll_fut)
async def timekeeper(self):
total_clock_time = int(time() - self.start_clock_time)
try:
while total_clock_time <= self.opt_timeout:
await asyncio.sleep(1)
total_clock_time = int(time() - self.start_clock_time)
except asyncio.CancelledError:
pass
def timeout(self, fut):
self.log("Reached TIMEOUT (%d seconds). Terminating all tasks." % self.opt_timeout)
self.status = "TIMEOUT"
self.terminate(timeout=True)
async def task_poller(self): async def task_poller(self):
if self.opt_timeout is not None:
timer_fut = asyncio.ensure_future(self.timekeeper())
timer_fut.add_done_callback(partial(SbyJob.timeout, self))
for task in self.tasks_pending: for task in self.tasks_pending:
await task.maybe_spawn_async() await task.maybe_spawn_async()
@ -358,16 +378,8 @@ class SbyJob:
if task.fut in done: if task.fut in done:
await task.shutdown_and_notify_async() await task.shutdown_and_notify_async()
# for task in self.tasks_pending: if self.opt_timeout is not None:
# await task.poll_async() timer_fut.cancel()
#if self.opt_timeout is not None:
#total_clock_time = int(time() - self.start_clock_time)
#if total_clock_time > self.opt_timeout:
#self.log("Reached TIMEOUT (%d seconds). Terminating all tasks." % self.opt_timeout)
#self.status = "TIMEOUT"
#self.terminate(timeout=True)
def log(self, logmessage): def log(self, logmessage):
tm = localtime() tm = localtime()