~lutostag/ubuntu/trusty/maas/1.5.4

« back to all changes in this revision

Viewing changes to src/maasserver/eventloop.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2014-02-15 12:08:23 UTC
  • mto: This revision was merged to the branch mainline in revision 48.
  • Revision ID: package-import@ubuntu.com-20140215120823-u7dkitfy0h8tbruh
Tags: upstream-1.5+bzr1948
ImportĀ upstreamĀ versionĀ 1.5+bzr1948

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
__metaclass__ = type
20
20
__all__ = [
 
21
    "services",
21
22
    "start",
22
23
    "stop",
23
24
]
25
26
from functools import wraps
26
27
from logging import getLogger
27
28
from os import getpid
 
29
from socket import gethostname
28
30
 
29
31
import crochet
30
32
from django.utils import autoreload
60
62
 
61
63
 
62
64
class RegionEventLoop:
 
65
    """An event loop running in a region controller process.
 
66
 
 
67
    Typically several processes will be running the web application -
 
68
    chiefly Django - across several machines, with multiple threads of
 
69
    execution in each process.
 
70
 
 
71
    This class represents a single event loop for each *process*,
 
72
    allowing convenient control of the event loop - a Twisted reactor
 
73
    running in a thread - and to which to attach and query services.
 
74
    """
63
75
 
64
76
    def __init__(self):
65
77
        super(RegionEventLoop, self).__init__()
68
80
 
69
81
    def init(self):
70
82
        """Spin up a Twisted event loop in this process."""
71
 
        if crochet.reactor.running:
72
 
            return  # This should only be called once.
73
 
        else:
 
83
        if not crochet.reactor.running:
74
84
            logger.info("Starting event loop in process %d", getpid())
75
85
            crochet.setup()
76
86
 
94
104
            crochet.reactor.removeSystemEventTrigger(handle)
95
105
        return self.services.stopService()
96
106
 
 
107
    @property
 
108
    def name(self):
 
109
        """A name for identifying this service in a distributed system."""
 
110
        return "%s:pid=%d" % (gethostname(), getpid())
 
111
 
97
112
 
98
113
loop = RegionEventLoop()
 
114
services = loop.services
99
115
start = loop.start
100
116
stop = loop.stop
101
117