# This file is part of Beremiz runtime.
# Copyright (C) 2018: Edouard TISSERANT
# See COPYING.Runtime file for copyrights details.
from __future__ import absolute_import
from threading import Lock, Condition, Thread
from six.moves import _thread
job to be executed by a worker
def __init__(self, call, *args, **kwargs):
self.job = (call, args, kwargs)
do the job by executing the call, and deal with exceptions
call, args, kwargs = self.job
self.result = call(*args, **kwargs)
self.exc_info = sys.exc_info()
serialize main thread load/unload of PLC shared objects
self.todo = Condition(self.mutex)
self.done = Condition(self.mutex)
self.free = Condition(self.mutex)
reraise exception happend in a job
@param job: job where original exception happend
exc_type = job.exc_info[0]
exc_value = job.exc_info[1]
exc_traceback = job.exc_info[2]
six.reraise(exc_type, exc_value, exc_traceback)
def runloop(self, *args, **kwargs):
meant to be called by worker thread (blocking)
self._threadID = _thread.get_ident()
_job = job(*args, **kwargs)
# _job.success can't be None after do()
def interleave(self, waker, *args, **kwargs):
as for twisted reactor's interleave, it passes all jobs to waker func
additionaly, it creates a new thread to wait for new job.
self.feed = Condition(self.mutex)
self._threadID = _thread.get_ident()
_job = job(*args, **kwargs)
self.own_thread = Thread(target = wakerfeedingloop)
def call(self, *args, **kwargs):
creates a job, execute it in worker thread, and deliver result.
if job execution raise exception, re-raise same exception
meant to be called by non-worker threads, but this is accepted.
_job = job(*args, **kwargs)
if self._threadID == _thread.get_ident():
# if caller is worker thread execute immediately
# otherwise notify and wait for completion
raise EOFError("Worker is disabled")
while self.job is not None:
raise EOFError("Worker job was interrupted")
unblocks main thread, and terminate execution of runloop()