runtime/Worker.py
author GP Orcullo <kinsamanka@gmail.com>
Sat, 04 Feb 2023 07:19:52 +0800
branchpython3
changeset 3784 a5f58ca4629b
parent 3753 5256e4bd92e6
child 3846 cf027bfe2653
permissions -rw-r--r--
fix name clash
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
     3
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
     4
# This file is part of Beremiz runtime.
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
     5
#
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
     6
# Copyright (C) 2018: Edouard TISSERANT
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
     7
#
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
     8
# See COPYING.Runtime file for copyrights details.
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
     9
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3642
diff changeset
    10
3584
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    11
from threading import Lock, Condition, Thread
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    12
3753
5256e4bd92e6 remove six.reraise
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
    13
import _thread
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    14
2309
d8fb90a2e11f Please pylint and pep8
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2307
diff changeset
    15
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    16
class job(object):
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    17
    """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    18
    job to be executed by a worker
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    19
    """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    20
    def __init__(self, call, *args, **kwargs):
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    21
        self.job = (call, args, kwargs)
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    22
        self.result = None
2604
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
    23
        self.success = None
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    24
        self.exc_info = None
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    25
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    26
    def do(self):
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    27
        """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    28
        do the job by executing the call, and deal with exceptions
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    29
        """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    30
        try:
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    31
            call, args, kwargs = self.job
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    32
            self.result = call(*args, **kwargs)
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    33
            self.success = True
3753
5256e4bd92e6 remove six.reraise
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
    34
        except Exception as e:
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    35
            self.success = False
3753
5256e4bd92e6 remove six.reraise
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
    36
            self.exc_info = e
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    37
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    38
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    39
class worker(object):
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    40
    """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    41
    serialize main thread load/unload of PLC shared objects
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    42
    """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    43
    def __init__(self):
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    44
        # Only one job at a time
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    45
        self._finish = False
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    46
        self._threadID = None
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    47
        self.mutex = Lock()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    48
        self.todo = Condition(self.mutex)
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    49
        self.done = Condition(self.mutex)
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    50
        self.free = Condition(self.mutex)
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    51
        self.job = None
2611
a1bf03277cec Fixed typo leading to some exception in some rare corner cases in worker.py
Edouard Tisserant
parents: 2604
diff changeset
    52
        self.enabled = False
3642
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
    53
        self.stopper = None
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
    54
        self.own_thread = None
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    55
2536
2747d6e72eb8 Fix invalid python3 syntax
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2492
diff changeset
    56
    def reraise(self, job):
2747d6e72eb8 Fix invalid python3 syntax
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2492
diff changeset
    57
        """
2747d6e72eb8 Fix invalid python3 syntax
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2492
diff changeset
    58
        reraise exception happend in a job
2747d6e72eb8 Fix invalid python3 syntax
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2492
diff changeset
    59
        @param job: job where original exception happend
2747d6e72eb8 Fix invalid python3 syntax
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2492
diff changeset
    60
        """
3753
5256e4bd92e6 remove six.reraise
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
    61
        raise job.exc_info
2536
2747d6e72eb8 Fix invalid python3 syntax
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2492
diff changeset
    62
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    63
    def runloop(self, *args, **kwargs):
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    64
        """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    65
        meant to be called by worker thread (blocking)
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    66
        """
2537
eb4a4cc41914 Fix various pylint and pep8 errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2536
diff changeset
    67
        self._threadID = _thread.get_ident()
2467
fce6ab7ae156 Enure that autostart blocks other @RunInMain PLCObject methods, and _unblocks_ them once autostart is done...
Edouard Tisserant
parents: 2463
diff changeset
    68
        self.mutex.acquire()
2604
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
    69
        self.enabled = True
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    70
        if args or kwargs:
2307
c44692b53736 Show more exceptions on stdout, particularly those that are raised by AutoLoad (first item in Main Thread worker)
Edouard Tisserant
parents: 2271
diff changeset
    71
            _job = job(*args, **kwargs)
c44692b53736 Show more exceptions on stdout, particularly those that are raised by AutoLoad (first item in Main Thread worker)
Edouard Tisserant
parents: 2271
diff changeset
    72
            _job.do()
2604
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
    73
            # _job.success can't be None after do()
2536
2747d6e72eb8 Fix invalid python3 syntax
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2492
diff changeset
    74
            if not _job.success:
2747d6e72eb8 Fix invalid python3 syntax
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2492
diff changeset
    75
                self.reraise(_job)
2486
44c2a4e2b84d Fixed deadlock in runtime's Worker. Was discovered while using WAMP and PYRO simultaneously.
Edouard Tisserant
parents: 2467
diff changeset
    76
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    77
        while not self._finish:
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    78
            self.todo.wait()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    79
            if self.job is not None:
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    80
                self.job.do()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    81
                self.done.notify()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    82
            else:
2486
44c2a4e2b84d Fixed deadlock in runtime's Worker. Was discovered while using WAMP and PYRO simultaneously.
Edouard Tisserant
parents: 2467
diff changeset
    83
                break
44c2a4e2b84d Fixed deadlock in runtime's Worker. Was discovered while using WAMP and PYRO simultaneously.
Edouard Tisserant
parents: 2467
diff changeset
    84
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    85
        self.mutex.release()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
    86
3642
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
    87
    def interleave(self, waker, stopper, *args, **kwargs):
3584
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    88
        """
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    89
        as for twisted reactor's interleave, it passes all jobs to waker func
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    90
        additionaly, it creates a new thread to wait for new job.
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    91
        """
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    92
        self.feed = Condition(self.mutex)
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    93
        self._threadID = _thread.get_ident()
3642
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
    94
        self.stopper = stopper
3584
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    95
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    96
        def wakerfeedingloop():
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    97
            self.mutex.acquire()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    98
            self.enabled = True
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
    99
            if args or kwargs:
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   100
                def first_job_todo():
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   101
                    _job = job(*args, **kwargs)
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   102
                    _job.do()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   103
                    if not _job.success:
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   104
                        self.reraise(_job)
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   105
                    self.mutex.acquire()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   106
                    self.feed.notify()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   107
                    self.mutex.release()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   108
                waker(first_job_todo)
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   109
                self.feed.wait()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   110
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   111
            while not self._finish:
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   112
                self.todo.wait()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   113
                def job_todo():
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   114
                    self.mutex.acquire()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   115
                    if self.job is not None:
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   116
                        self.job.do()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   117
                        self.feed.notify()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   118
                        self.done.notify()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   119
                    self.mutex.release()
3642
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
   120
                if self._finish:
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
   121
                    break
3584
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   122
                waker(job_todo)
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   123
                self.feed.wait()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   124
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   125
            self.mutex.release()
3585
efdefbad49eb runtime: fix bugs introduced in previous commit, and refactor some code
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3584
diff changeset
   126
        self.own_thread = Thread(target = wakerfeedingloop)
efdefbad49eb runtime: fix bugs introduced in previous commit, and refactor some code
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3584
diff changeset
   127
        self.own_thread.start()
3584
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   128
3585
efdefbad49eb runtime: fix bugs introduced in previous commit, and refactor some code
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3584
diff changeset
   129
    def stop(self):
3584
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   130
        """
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   131
        !interleave
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   132
        """
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   133
        self.mutex.acquire()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   134
        self._finish = True
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   135
        self.enabled = False
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   136
        self.job = None
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   137
        self.todo.notify()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   138
        self.done.notify()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   139
        self.mutex.release()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   140
        self.own_thread.join()
8a54fd58a552 Runtime: wx.app.mainLoop and twisted reactor now share main thread with runtime.Worker.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2611
diff changeset
   141
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   142
    def call(self, *args, **kwargs):
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   143
        """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   144
        creates a job, execute it in worker thread, and deliver result.
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   145
        if job execution raise exception, re-raise same exception
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   146
        meant to be called by non-worker threads, but this is accepted.
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   147
        blocking until job done
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   148
        """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   149
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   150
        _job = job(*args, **kwargs)
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   151
2537
eb4a4cc41914 Fix various pylint and pep8 errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2536
diff changeset
   152
        if self._threadID == _thread.get_ident():
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   153
            # if caller is worker thread execute immediately
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   154
            _job.do()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   155
        else:
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   156
            # otherwise notify and wait for completion
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   157
            self.mutex.acquire()
2604
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
   158
            if not self.enabled:
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
   159
                self.mutex.release()
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
   160
                raise EOFError("Worker is disabled")
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   161
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   162
            while self.job is not None:
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   163
                self.free.wait()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   164
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   165
            self.job = _job
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   166
            self.todo.notify()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   167
            self.done.wait()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   168
            self.job = None
2486
44c2a4e2b84d Fixed deadlock in runtime's Worker. Was discovered while using WAMP and PYRO simultaneously.
Edouard Tisserant
parents: 2467
diff changeset
   169
            self.free.notify()
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   170
            self.mutex.release()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   171
2604
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
   172
        if _job.success is None:
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
   173
            raise EOFError("Worker job was interrupted")
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
   174
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   175
        if _job.success:
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   176
            return _job.result
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   177
        else:
2536
2747d6e72eb8 Fix invalid python3 syntax
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2492
diff changeset
   178
            self.reraise(_job)
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   179
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   180
    def quit(self):
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   181
        """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   182
        unblocks main thread, and terminate execution of runloop()
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   183
        """
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   184
        # mark queue
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   185
        self._finish = True
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   186
        self.mutex.acquire()
2604
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
   187
        self.enabled = False
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   188
        self.job = None
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   189
        self.todo.notify()
2604
c8a25a3a7f8b Runtime worker : unblock the last waiting job and prevent any new job to wait when Main Worker is being shut down.
Edouard Tisserant
parents: 2537
diff changeset
   190
        self.done.notify()
2270
d9175daf6522 Refactoring. Separated PLC Object, PYRO Server and MainWorker :
Edouard Tisserant
parents:
diff changeset
   191
        self.mutex.release()
3642
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
   192
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
   193
    def finish(self):
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
   194
        if self.own_thread is None:
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
   195
            self.quit()
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
   196
        if self.stopper is not None:
cd3d15e8ef42 Runtime: fix PLC not terminating when being Repaired (i.e purged).
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3585
diff changeset
   197
            self.stopper()