/usr/lib/python2.7/dist-packages/circuits/core/manager.py is in python-circuits 3.1.0+ds1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 | # Package: manager
# Date: 11th April 2010
# Author: James Mills, prologic at shortcircuit dot net dot au
"""
This module defines the Manager class.
"""
import atexit
from os import getpid, kill
from inspect import isfunction
from uuid import uuid4 as uuid
from operator import attrgetter
from types import GeneratorType
from itertools import chain, count
from signal import SIGINT, SIGTERM
from heapq import heappush, heappop
from weakref import WeakValueDictionary
from traceback import format_exc, format_tb
from sys import exc_info as _exc_info, stderr
from signal import signal as set_signal_handler
from threading import current_thread, Thread, RLock
from multiprocessing import current_process, Process
try:
from signal import SIGKILL
except ImportError:
SIGKILL = SIGTERM
from .values import Value
from ..tools import tryimport
from .handlers import handler
from ..six import create_bound_method, next
from .events import exception, generate_events, signal, started, stopped, Event
thread = tryimport(("thread", "_thread"))
TIMEOUT = 0.1 # 100ms timeout when idle
class UnregistrableError(Exception):
"""Raised if a component cannot be registered as child."""
class TimeoutError(Exception):
"""Raised if wait event timeout occurred"""
class CallValue(object):
def __init__(self, value):
self.value = value
class ExceptionWrapper(object):
def __init__(self, exception):
self.exception = exception
def extract(self):
return self.exception
class Dummy(object):
channel = None
_dummy = Dummy()
del Dummy
class Manager(object):
"""
The manager class has two roles. As a base class for component
implementation, it provides methods for event and handler management.
The method :meth:`.fireEvent` appends a new event at the end of the event
queue for later execution. :meth:`.waitEvent` suspends the execution
of a handler until all handlers for a given event have been invoked.
:meth:`.callEvent` combines the last two methods in a single method.
The methods :meth:`.addHandler` and :meth:`.removeHandler` allow handlers
for events to be added and removed dynamically. (The more common way to
register a handler is to use the :func:`~.handlers.handler` decorator
or derive the class from :class:`~.components.Component`.)
In its second role, the :class:`.Manager` takes the role of the
event executor. Every component hierarchy has a root component that
maintains a queue of events. Firing an event effectively means
appending it to the event queue maintained by the root manager.
The :meth:`.flush` method removes all pending events from the
queue and, for each event, invokes all the handlers. Usually,
:meth:`.flush` is indirectly invoked by :meth:`run`.
The manager optionally provides information about the execution of
events as automatically generated events. If an :class:`~.events.Event`
has its :attr:`success` attribute set to True, the manager fires
a :class:`~.events.Success` event if all handlers have been
executed without error. Note that this event will be
enqueued (and dispatched) immediately after the events that have been
fired by the event's handlers. So the success event indicates both
the successful invocation of all handlers for the event and the
processing of the immediate follow-up events fired by those handlers.
Sometimes it is not sufficient to know that an event and its
immediate follow-up events have been processed. Rather, it is
important to know when all state changes triggered by an event,
directly or indirectly, have been performed. This also includes
the processing of events that have been fired when invoking
the handlers for the follow-up events and the processing of events
that have again been fired by those handlers and so on. The completion
of the processing of an event and all its direct or indirect
follow-up events may be indicated by a :class:`~.events.Complete`
event. This event is generated by the manager if :class:`~.events.Event`
has its :attr:`complete` attribute set to True.
Apart from the event queue, the root manager also maintains a list of
tasks, actually Python generators, that are updated when the event queue
has been flushed.
"""
_currently_handling = None
"""
The event currently being handled.
"""
def __init__(self, *args, **kwargs):
"initializes x; see x.__class__.__doc__ for signature"
self._queue = []
self._counter = count()
self._tasks = set()
self._cache = dict()
self._globals = set()
self._handlers = dict()
self._flush_batch = 0
self._cache_needs_refresh = False
self._values = WeakValueDictionary()
self._executing_thread = None
self._flushing_thread = None
self._running = False
self.__thread = None
self.__process = None
self._lock = RLock()
self.root = self.parent = self
self.components = set()
def __repr__(self):
"x.__repr__() <==> repr(x)"
name = self.__class__.__name__
channel = "/{0:s}".format(str(getattr(self, "channel", "")))
q = len(self._queue)
state = "R" if self.running else "S"
pid = current_process().pid
if pid:
id = "%s:%s" % (pid, current_thread().getName())
else:
id = current_thread().getName()
format = "<%s%s %s (queued=%d) [%s]>"
return format % (name, channel, id, q, state)
def __contains__(self, y):
"""x.__contains__(y) <==> y in x
Return True if the Component y is registered.
"""
components = self.components.copy()
return y in components or y in [c.__class__ for c in components]
def __len__(self):
"""x.__len__() <==> len(x)
Returns the number of events in the Event Queue.
"""
return len(self._queue)
def __add__(self, y):
"""x.__add__(y) <==> x+y
(Optional) Convenience operator to register y with x
Equivalent to: y.register(x)
@return: x
@rtype Component or Manager
"""
y.register(self)
return self
def __iadd__(self, y):
"""x.__iadd__(y) <==> x += y
(Optional) Convenience operator to register y with x
Equivalent to: y.register(x)
@return: x
@rtype Component or Manager
"""
y.register(self)
return self
def __sub__(self, y):
"""x.__sub__(y) <==> x-y
(Optional) Convenience operator to unregister y from x.manager
Equivalent to: y.unregister()
@return: x
@rtype Component or Manager
"""
if y.manager is not y:
y.unregister()
return self
def __isub__(self, y):
"""x.__sub__(y) <==> x -= y
(Optional) Convenience operator to unregister y from x
Equivalent to: y.unregister()
@return: x
@rtype Component or Manager
"""
if y.manager is not y:
y.unregister()
return self
@property
def name(self):
"""Return the name of this Component/Manager"""
return self.__class__.__name__
@property
def running(self):
"""Return the running state of this Component/Manager"""
return self._running
@property
def pid(self):
"""Return the process id of this Component/Manager"""
return getpid() if self.__process is None else self.__process.pid
def getHandlers(self, event, channel, **kwargs):
name = event.name
handlers = set()
_handlers = set()
_handlers.update(self._handlers.get("*", []))
_handlers.update(self._handlers.get(name, []))
for _handler in _handlers:
handler_channel = _handler.channel
if handler_channel is None:
# XXX: Why do we care about the event handler's channel?
# This probably costs us performance for what?
# I've not ever had to rely on this in practice...
handler_channel = getattr(
getattr(
_handler, "im_self", getattr(
_handler, "__self__", _dummy
)
),
"channel", None
)
if channel == "*" or handler_channel in ("*", channel,) \
or channel is self:
handlers.add(_handler)
if not kwargs.get("exclude_globals", False):
handlers.update(self._globals)
for c in self.components.copy():
handlers.update(c.getHandlers(event, channel, **kwargs))
return handlers
def addHandler(self, f):
method = create_bound_method(f, self) if isfunction(f) else f
setattr(self, method.__name__, method)
if not method.names and method.channel == "*":
self._globals.add(method)
elif not method.names:
self._handlers.setdefault("*", set()).add(method)
else:
for name in method.names:
self._handlers.setdefault(name, set()).add(method)
self.root._cache_needs_refresh = True
return method
def removeHandler(self, method, event=None):
if event is None:
names = method.names
else:
names = [event]
for name in names:
self._handlers[name].remove(method)
if not self._handlers[name]:
del self._handlers[name]
try:
delattr(self, method.__name__)
except AttributeError:
# Handler was never part of self
pass
self.root._cache_needs_refresh = True
def registerChild(self, component):
if component._executing_thread is not None:
if self.root._executing_thread is not None:
raise UnregistrableError()
self.root._executing_thread = component._executing_thread
component._executing_thread = None
self.components.add(component)
self.root._queue.extend(list(component._queue))
component._queue = []
self.root._cache_needs_refresh = True
def unregisterChild(self, component):
self.components.remove(component)
self.root._cache_needs_refresh = True
def _fire(self, event, channel, priority=0):
# check if event is fired while handling an event
if thread.get_ident() == (self._executing_thread or \
self._flushing_thread) and not isinstance(event, signal):
if self._currently_handling is not None and \
getattr(self._currently_handling, "cause", None):
# if the currently handled event wants to track the
# events generated by it, do the tracking now
event.cause = self._currently_handling
event.effects = 1
self._currently_handling.effects += 1
heappush(
self._queue,
(
priority,
next(self._counter),
(event, channel)
)
)
# the event comes from another thread
else:
# Another thread has provided us with something to do.
# If the component is running, we must make sure that
# any pending generate event waits no longer, as there
# is something to do now.
with self._lock:
# Modifications of attribute self._currently_handling
# (in _dispatch()), calling reduce_time_left(0). and adding an
# event to the (empty) event queue must be atomic, so we have
# to lock. We can save the locking around
# self._currently_handling = None though, but then need to copy
# it to a local variable here before performing a sequence of
# operations that assume its value to remain unchanged.
handling = self._currently_handling
if isinstance(handling, generate_events):
heappush(
self._queue,
(
priority,
next(self._counter),
(event, channel)
)
)
handling.reduce_time_left(0)
else:
heappush(
self._queue,
(
priority,
next(self._counter),
(event, channel)
)
)
def fireEvent(self, event, *channels, **kwargs):
"""Fire an event into the system.
:param event: The event that is to be fired.
:param channels: The channels that this event is delivered on.
If no channels are specified, the event is delivered to the
channels found in the event's :attr:`channel` attribute.
If this attribute is not set, the event is delivered to
the firing component's channel. And eventually,
when set neither, the event is delivered on all
channels ("*").
"""
if not channels:
channels = event.channels \
or (getattr(self, "channel", "*"),) \
or ("*",)
event.channels = channels
event.value = Value(event, self)
self.root._fire(event, channels, **kwargs)
return event.value
fire = fireEvent
def registerTask(self, g):
self.root._tasks.add(g)
def unregisterTask(self, g):
if g in self.root._tasks:
self.root._tasks.remove(g)
def waitEvent(self, event, *channels, **kwargs):
if isinstance(event, Event):
event_object = event
event_name = event.name
else:
event_object = None
event_name = event
state = {
'run': False,
'flag': False,
'event': None,
'timeout': kwargs.get("timeout", -1)
}
def _on_event(self, event, *args, **kwargs):
if not state['run'] and (
event_object is None or event is event_object
):
self.removeHandler(_on_event_handler, event_name)
event.alert_done = True
state['run'] = True
state['event'] = event
def _on_done(self, event, *args, **kwargs):
if state['event'] == event.parent:
state['flag'] = True
self.registerTask((state['task_event'],
state['task'],
state['parent']))
if state['timeout'] > 0:
self.removeHandler(
state['tick_handler'],
"generate_events"
)
def _on_tick(self):
if state['timeout'] == 0:
self.registerTask(
(
state['task_event'],
(e for e in (ExceptionWrapper(TimeoutError()),)),
state['parent']
)
)
self.removeHandler(_on_done_handler, "%s_done" % event_name)
self.removeHandler(_on_tick_handler, "generate_events")
elif state['timeout'] > 0:
state['timeout'] -= 1
if not channels:
channels = (None,)
for channel in channels:
_on_event_handler = self.addHandler(
handler(event_name, channel=channel)(_on_event))
_on_done_handler = self.addHandler(
handler("%s_done" % event_name, channel=channel)(_on_done))
if state['timeout'] >= 0:
_on_tick_handler = state['tick_handler'] = self.addHandler(
handler("generate_events", channel=channel)(_on_tick))
yield state
if not state['timeout']:
self.removeHandler(_on_done_handler, "%s_done" % event_name)
if state["event"] is not None:
yield CallValue(state["event"].value)
wait = waitEvent
def callEvent(self, event, *channels, **kwargs):
"""
Fire the given event to the specified channels and suspend
execution until it has been dispatched. This method may only
be invoked as argument to a ``yield`` on the top execution level
of a handler (e.g. "``yield self.callEvent(event)``").
It effectively creates and returns a generator
that will be invoked by the main loop until the event has
been dispatched (see :func:`circuits.core.handlers.handler`).
"""
value = self.fire(event, *channels)
for r in self.waitEvent(event, *event.channels, **kwargs):
yield r
yield CallValue(value)
call = callEvent
def _flush(self):
# Handle events currently on queue, but none of the newly generated
# events. Note that _flush can be called recursively.
old_flushing = self._flushing_thread
try:
self._flushing_thread = thread.get_ident()
if self._flush_batch == 0:
self._flush_batch = len(self._queue)
while self._flush_batch > 0:
self._flush_batch -= 1 # Decrement first!
priority, count, (event, channels) = heappop(self._queue)
self._dispatcher(event, channels, self._flush_batch)
finally:
self._flushing_thread = old_flushing
def flushEvents(self):
"""
Flush all Events in the Event Queue. If called on a manager
that is not the root of an object hierarchy, the invocation
is delegated to the root manager.
"""
self.root._flush()
flush = flushEvents
def _dispatcher(self, event, channels, remaining):
if event.cancelled:
return
if event.complete:
if not getattr(event, "cause", None):
event.cause = event
event.effects = 1 # event itself counts (must be done)
eargs = event.args
ekwargs = event.kwargs
if self._cache_needs_refresh:
# Don't call self._cache.clear() from other threads,
# this may interfere with cache rebuild.
self._cache.clear()
self._cache_needs_refresh = False
try: # try/except is fastest if successful in most cases
handlers = self._cache[(event.name, channels)]
except KeyError:
h = (self.getHandlers(event, channel) for channel in channels)
handlers = sorted(
chain(*h),
key=attrgetter("priority"),
reverse=True
)
if isinstance(event, generate_events):
from .helpers import FallBackGenerator
handlers.append(FallBackGenerator()._on_generate_events)
elif isinstance(event, exception) and len(handlers) == 0:
from .helpers import FallBackExceptionHandler
handlers.append(FallBackExceptionHandler()._on_exception)
elif isinstance(event, signal) and len(handlers) == 0:
from .helpers import FallBackSignalHandler
handlers.append(FallBackSignalHandler()._on_signal)
self._cache[(event.name, channels)] = handlers
if isinstance(event, generate_events):
with self._lock:
self._currently_handling = event
if remaining > 0 or len(self._queue) or not self._running:
event.reduce_time_left(0)
elif self._tasks:
event.reduce_time_left(TIMEOUT)
# From now on, firing an event will reduce time left
# to 0, which prevents handlers from waiting (or wakes
# them up with resume if they should be waiting already)
else:
self._currently_handling = event
value = None
err = None
for handler in handlers:
event.handler = handler
try:
if handler.event:
value = handler(event, *eargs, **ekwargs)
else:
value = handler(*eargs, **ekwargs)
except (KeyboardInterrupt, SystemExit):
self.stop()
except:
etype, evalue, etraceback = _exc_info()
traceback = format_tb(etraceback)
err = (etype, evalue, traceback)
event.value.errors = True
value = err
if event.failure:
self.fire(
event.child("failure", event, err),
*event.channels
)
self.fire(
exception(
etype, evalue, traceback,
handler=handler, fevent=event
)
)
if value is not None:
if isinstance(value, GeneratorType):
event.waitingHandlers += 1
event.value.promise = True
self.registerTask((event, value, None))
else:
event.value.value = value
# it is kind of a temporal hack to allow processing
# of tasks, added in one of handlers here
if isinstance(event, generate_events) and self._tasks:
event.reduce_time_left(TIMEOUT)
if event.stopped:
break # Stop further event processing
self._currently_handling = None
self._eventDone(event, err)
def _eventDone(self, event, err=None):
if event.waitingHandlers:
return
# The "%s_Done" event is for internal use by waitEvent only.
# Use the "%s_Success" event in you application if you are
# interested in being notified about the last handler for
# an event having been invoked.
if event.alert_done:
self.fire(event.child("done", event.value.value), *event.channels)
if err is None and event.success:
channels = getattr(event, "success_channels", event.channels)
self.fire(
event.child("success", event, event.value.value), *channels
)
while True:
# cause attributes indicates interest in completion event
cause = getattr(event, "cause", None)
if not cause:
break
# event takes part in complete detection (as nested or root event)
event.effects -= 1
if event.effects > 0:
break # some nested events remain to be completed
if event.complete: # does this event want signaling?
self.fire(
event.child("complete", event, event.value.value),
*getattr(event, "complete_channels", event.channels)
)
# this event and nested events are done now
delattr(event, "cause")
delattr(event, "effects")
# cause has one of its nested events done, decrement and check
event = cause
def _signal_handler(self, signo, stack):
self.fire(signal(signo, stack))
def start(self, process=False, link=None):
"""
Start a new thread or process that invokes this manager's
``run()`` method. The invocation of this method returns
immediately after the task or process has been started.
"""
if process:
# Parent<->Child Bridge
if link is not None:
from circuits.net.sockets import Pipe
from circuits.core.bridge import Bridge
channels = (uuid(),) * 2
parent, child = Pipe(*channels)
bridge = Bridge(parent, channel=channels[0]).register(link)
args = (child,)
else:
args = ()
bridge = None
self.__process = Process(
target=self.run, args=args, name=self.name
)
self.__process.daemon = True
self.__process.start()
return self.__process, bridge
else:
self.__thread = Thread(target=self.run, name=self.name)
self.__thread.daemon = True
self.__thread.start()
return self.__thread, None
def join(self):
if getattr(self, "_thread", None) is not None:
return self.__thread.join()
if getattr(self, "_process", None) is not None:
return self.__process.join()
def stop(self):
"""
Stop this manager. Invoking this method causes
an invocation of ``run()`` to return.
"""
if self.__process is not None and self.__process.is_alive():
self.__process.terminate()
self.__process.join(TIMEOUT)
if self.__process.is_alive():
kill(self.__process.pid, SIGKILL)
if not self.running:
return
self._running = False
self.fire(stopped(self))
if self.root._executing_thread is None:
for _ in range(3):
self.tick()
def processTask(self, event, task, parent=None):
value = None
try:
value = next(task)
if isinstance(value, CallValue):
# Done here, next() will StopIteration anyway
self.unregisterTask((event, task, parent))
# We are in a callEvent
value = parent.send(value.value)
if isinstance(value, GeneratorType):
# We loose a yield but we gain one,
# we don't need to change
# event.waitingHandlers
# The below code is delegated to handlers
# in the waitEvent generator
# self.registerTask((event, value, parent))
task_state = next(value)
task_state['task_event'] = event
task_state['task'] = value
task_state['parent'] = parent
else:
event.waitingHandlers -= 1
if value is not None:
event.value.value = value
self.registerTask((event, parent, None))
elif isinstance(value, GeneratorType):
event.waitingHandlers += 1
self.unregisterTask((event, task, None))
# First yielded value is always the task state
task_state = next(value)
task_state['task_event'] = event
task_state['task'] = value
task_state['parent'] = task
# The below code is delegated to handlers
# in the waitEvent generator
# self.registerTask((event, value, task))
# XXX: ^^^ Why is this commented out anyway?
elif isinstance(value, ExceptionWrapper):
self.unregisterTask((event, task, parent))
if parent:
value = parent.throw(value.extract())
if value is not None:
value_generator = (val for val in (value,))
self.registerTask((event, value_generator, parent))
else:
raise value.extract()
elif value is not None:
event.value.value = value
except StopIteration:
event.waitingHandlers -= 1
self.unregisterTask((event, task, parent))
if parent:
self.registerTask((event, parent, None))
elif event.waitingHandlers == 0:
event.value.inform(True)
self._eventDone(event)
except (KeyboardInterrupt, SystemExit):
self.stop()
except:
self.unregisterTask((event, task, parent))
etype, evalue, etraceback = _exc_info()
traceback = format_tb(etraceback)
err = (etype, evalue, etraceback)
event.value.value = err
event.value.errors = True
event.value.inform(True)
if event.failure:
self.fire(event.child("failure", event, err), *event.channels)
self.fire(
exception(
etype, evalue, traceback,
handler=None, fevent=event
)
)
def tick(self, timeout=-1):
"""
Execute all possible actions once. Process all registered tasks
and flush the event queue. If the application is running fire a
GenerateEvents to get new events from sources.
This method is usually invoked from :meth:`~.run`. It may also be
used to build an application specific main loop.
:param timeout: the maximum waiting time spent in this method. If
negative, the method may block until at least one action
has been taken.
:type timeout: float, measuring seconds
"""
# process tasks
if self._tasks:
for task in self._tasks.copy():
self.processTask(*task)
if self._running:
self.fire(generate_events(self._lock, timeout), "*")
self._queue and self.flush()
def run(self, socket=None):
"""
Run this manager. The method fires the
:class:`~.events.Started` event and then continuously
calls :meth:`~.tick`.
The method returns when the manager's
:meth:`~.stop` method is invoked.
If invoked by a programs main thread, a signal handler for
the ``INT`` and ``TERM`` signals is installed. This handler
fires the corresponding :class:`~.events.Signal`
events and then calls :meth:`~.stop` for the manager.
"""
atexit.register(self.stop)
if current_thread().getName() == "MainThread":
try:
set_signal_handler(SIGINT, self._signal_handler)
set_signal_handler(SIGTERM, self._signal_handler)
except ValueError:
# Ignore if we can't install signal handlers
pass
self._running = True
self.root._executing_thread = current_thread()
# Setup Communications Bridge
if socket is not None:
from circuits.core.bridge import Bridge
Bridge(socket, channel=socket.channel).register(self)
self.fire(started(self))
try:
while self.running or len(self._queue):
self.tick()
# Fading out, handle remaining work from stop event
for _ in range(3):
self.tick()
except Exception as e:
stderr.write("Unhandled ERROR: {0:s}\n".format(str(e)))
stderr.write(format_exc())
finally:
try:
self.tick()
except:
pass
self.root._executing_thread = None
self.__thread = None
self.__process = None
|