2014-10-05 16:25:31 +08:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from artiq.management.worker import Worker
|
|
|
|
|
|
|
|
|
|
|
|
class Scheduler:
|
2014-12-08 19:22:02 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.worker = Worker(*args, **kwargs)
|
2014-12-09 16:26:50 +08:00
|
|
|
self.queued = []
|
|
|
|
self.queue_count = asyncio.Semaphore(0)
|
2014-10-05 16:25:31 +08:00
|
|
|
|
2014-10-23 18:48:03 +08:00
|
|
|
@asyncio.coroutine
|
|
|
|
def start(self):
|
|
|
|
self.task = asyncio.Task(self._schedule())
|
|
|
|
yield from self.worker.create_process()
|
2014-10-05 16:25:31 +08:00
|
|
|
|
2014-10-23 18:48:03 +08:00
|
|
|
@asyncio.coroutine
|
|
|
|
def stop(self):
|
|
|
|
self.task.cancel()
|
|
|
|
yield from asyncio.wait([self.task])
|
|
|
|
del self.task
|
|
|
|
yield from self.worker.end_process()
|
2014-10-05 16:25:31 +08:00
|
|
|
|
2014-10-23 18:48:03 +08:00
|
|
|
def run_once(self, run_params, timeout):
|
2014-12-09 16:26:50 +08:00
|
|
|
self.queued.append((run_params, timeout))
|
|
|
|
self.queue_count.release()
|
2014-10-05 16:25:31 +08:00
|
|
|
|
2014-10-23 18:48:03 +08:00
|
|
|
@asyncio.coroutine
|
|
|
|
def _schedule(self):
|
2014-10-05 16:25:31 +08:00
|
|
|
while True:
|
2014-12-09 16:26:50 +08:00
|
|
|
yield from self.queue_count.acquire()
|
|
|
|
run_params, timeout = self.queued.pop(0)
|
2014-10-23 18:48:03 +08:00
|
|
|
result = yield from self.worker.run(run_params, timeout)
|
2014-10-05 16:25:31 +08:00
|
|
|
print(result)
|