Ejemplo n.º 1
0
 def __init__(self):
     if self.parent is not None:
         raise RuntimeError('Hub must be created in the root fiber')
     super(Hub, self).__init__(target=self.run)
     self.name = 'Hub'
     self.context = ''
     self._loop = pyuv.Loop()
     self._loop.excepthook = self._uncaught_exception
     self._data = {}
     self._noswitch_depth = 0
     self._callbacks = collections.deque()
     # Thread IDs may be recycled when a thread exits. But as long as the
     # hub is alive, it won't be recycled so in that case we can use just
     # the ID as a check whether we are in the same thread or not.
     self._thread = compat.get_thread_ident()
     self._async = pyuv.Async(self._loop, lambda h: self._loop.stop())
     self._sigint = pyuv.Signal(self._loop)
     self._sigint.start(self._on_sigint, signal.SIGINT)
     # Mark our own handles as "system handles". This allows the test suite
     # to check that no active handles except these escape from tests.
     self._async._system_handle = True
     self._sigint._system_handle = True
     self._log = logging.get_logger()
     self._log.debug('new Hub for {.name}', threading.current_thread())
     self._closing = False
     self._error = None
Ejemplo n.º 2
0
    def _adapter_connect(self):
        """Connect to the remote socket, adding the socket to the IOLoop if
        connected

        :rtype: bool

        """
        LOGGER.debug('init io and signal watchers if any')
        # reuse existing signal watchers, can only be declared for 1 ioloop
        global global_sigint_watcher, global_sigterm_watcher
        error = super(LibuvConnection, self)._adapter_connect()

        if not error:
            if self._on_signal_callback and not global_sigterm_watcher:
                global_sigint_watcher = pyuv.Signal(self.ioloop)
            if self._on_signal_callback and not global_sigint_watcher:
                global_sigterm_watcher = pyuv.Signal(self.ioloop)

            if not self._io_watcher:
                self._io_watcher = pyuv.Poll(self.ioloop, self.socket.fileno())
                self._io_watcher.fd = self.socket.fileno()

            self. async = pyuv.Async(self.ioloop, self._noop_callable())
            self. async .send()
            if self._on_signal_callback:
                global_sigterm_watcher.start(signal.SIGTERM,
                                             self._handle_sigterm)
                global_sigint_watcher.start(signal.SIGINT, self._handle_sigint)
            self._io_watcher.start(self._PIKA_TO_LIBUV_ARRAY[self.event_state],
                                   self._handle_events)

        return error
Ejemplo n.º 3
0
    def __init__(self):
        if getattr(_tls, 'loop', None) is not None:
            raise RuntimeError(
                'cannot instantiate more than one event loop per thread')
        _tls.loop = self
        self._loop = pyuv.Loop()
        self._loop.excepthook = self._handle_error
        self._loop.event_loop = self
        self._threadpool = ThreadPool(self)
        self.task = Fiber(self._run_loop)

        self._destroyed = False
        self._started = False
        self._running = False

        self._fd_map = dict()
        self._signals = dict()
        self._timers = set()
        self._ready = deque()

        self._ready_processor = pyuv.Idle(self._loop)
        self._waker = pyuv.Async(self._loop, self._async_cb)
        self._waker.unref()

        self._install_signal_checker()
Ejemplo n.º 4
0
    def __init__(self, loop=None):

        # by default we run on the default loop
        self.loop = loop or pyuv.Loop.default_loop()

        # wakeup ev for internal signaling
        self._wakeup_ev = pyuv.Async(self.loop, self._on_wakeup)

        # initialize the emitter
        self._emitter = EventEmitter(self.loop)

        # initialize the process tracker
        self._tracker = ProcessTracker(self.loop)

        # initialize some values
        self.apps = None
        self.started = False
        self._stop_ev = None
        self.max_process_id = 0
        self.processes = OrderedDict()
        self.running = OrderedDict()
        self.groups = {}
        self.channel = deque()
        self._updates = deque()
        self._signals = []

        self.stopping = False
        self.stop_cb = None
        self.restart_cb = None
        self._lock = RLock()
Ejemplo n.º 5
0
    def __init__(self):
        global _tls
        if getattr(_tls, 'loop', None) is not None:
            raise RuntimeError('cannot instantiate more than one event loop per thread')
        _tls.loop = self
        self._loop = pyuv.Loop()
        self._loop.excepthook = self._handle_error
        self._loop.event_loop = self
        self._default_executor = None
        self._threadpool = ThreadPool(self)
        self.tasklet = tasklet(self._run_loop)

        self._started = False

        self._fd_map = dict()
        self._signals = dict()
        self._timers = set()
        self._ready = deque()

        self._ready_processor = pyuv.Check(self._loop)
        self._ready_processor.start(self._process_ready)
        self._ready_processor.unref()

        self._ticker = Ticker(self._loop)

        self._waker = pyuv.Async(self._loop, self._ticker.tick)
        self._waker.unref()

        self._install_signal_checker()
Ejemplo n.º 6
0
    def test_async2(self):
        self.prepare_cb_called = 0
        self.check_cb_called = 0

        def prepare_cb(prepare):
            self.prepare_cb_called += 1
            self.thread = threading.Thread(target=thread_cb)
            self.thread.start()

        def check_cb(check):
            self.check_cb_called += 1
            self.loop.stop()

        def thread_cb():
            time.sleep(0.01)
            self. async .send()

        self. async = pyuv.Async(self.loop)
        self.prepare = pyuv.Prepare(self.loop)
        self.prepare.start(prepare_cb)
        self.check = pyuv.Check(self.loop)
        self.check.start(check_cb)
        self.loop.run()
        self.assertEqual(self.prepare_cb_called, 1)
        self.assertEqual(self.check_cb_called, 1)
Ejemplo n.º 7
0
 def test_async1(self):
     self.async_cb_called = 0
     self.prepare_cb_called = 0
     def async_cb(async):
         with self.lock:
             self.async_cb_called += 1
             n = self.async_cb_called
         if n == 3:
             self.async.close()
             self.prepare.close()
     def prepare_cb(prepare):
         if self.prepare_cb_called:
             return
         self.prepare_cb_called += 1
         self.thread = threading.Thread(target=thread_cb)
         self.thread.start()
     def thread_cb():
         while True:
             with self.lock:
                 n = self.async_cb_called
                 if n == 3:
                     break
                 self.async.send()
     self.async = pyuv.Async(self.loop, async_cb)
     self.prepare = pyuv.Prepare(self.loop)
     self.prepare.start(prepare_cb)
     self.lock = threading.Lock()
     self.loop.run()
     self.assertEqual(self.async_cb_called, 3)
     self.assertEqual(self.prepare_cb_called, 1)
Ejemplo n.º 8
0
 def __init__(self):
     self._loop = pyuv.Loop.default_loop()
     self._running = False
     self._stopped = False
     self._callbacks = collections.deque()
     self._callback_processor = pyuv.Check(self._loop)
     self._callback_processor.start(self._process_callbacks)
     self._waker = pyuv.Async(self._loop, lambda x: None)
Ejemplo n.º 9
0
 def __init__(self):
     super(Manager, self).__init__()
     self.loop = pyuv.Loop()
     self.neighbors = {}
     self.ports = {}
     self.daemon = True
     self.wake = pyuv.Async(self.loop, self.sync)
     self.queue = Queue()
Ejemplo n.º 10
0
 def __init__(self, loop):
     self.loop = loop
     self._queue = deque()
     self._dispatcher = pyuv.Prepare(self.loop)
     self._dispatcher.start(self._send)
     if hasattr(self._dispatcher, 'unref'):
         self._dispatcher.unref()
     self._tick = pyuv.Async(loop, self._spin_up)
     self._spinner = pyuv.Idle(self.loop)
Ejemplo n.º 11
0
Archivo: uv.py Proyecto: BigRLab/flower
    def __init__(self):
        self.loop = pyuv.Loop()
        self._async = pyuv.Async(self.loop, self._wakeloop)
        self._async.unref()
        self.fds = {}
        self._lock = threading.RLock()
        self.running = False

        # start the server task
        self._runtask = tasklet(self.run, "uv_server")()
Ejemplo n.º 12
0
 def wait(self):
     """Wait for signal or until set call."""
     if self._flag:
         return
     self.start()
     self._guard = pyuv.Async(self._loop, lambda h: None)
     try:
         self._loop.run()
     finally:
         self.stop()
Ejemplo n.º 13
0
 def __init__(self, loop, pid, ref=True):
     if not loop.default:
         raise TypeError("child watchers are only allowed in the default loop")
     super(Child, self).__init__(loop, ref)
     loop.install_sigchld()
     self._active = False
     self._pid = pid
     self.rpid = None
     self.rstatus = None
     self._handle = pyuv.Async(self.loop._loop, self._async_cb)
Ejemplo n.º 14
0
 def __init__(self, address):
     self.sock = socket.socket()
     self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.sock.bind(address)
     self.sock.setblocking(0)
     self.address = self.sock.getsockname()
     self.loop = pyuv.Loop.default_loop()
     self.poll_watcher = pyuv.Poll(self.loop, self.sock.fileno())
     self.async = pyuv.Async(self.loop, self.async_cb)
     self.conns = weakref.WeakValueDictionary()
     self.signal_watchers = set()
Ejemplo n.º 15
0
 def test_signal1(self):
     self.async_cb_called = 0
     self.signal_cb_called = 0
     self. async = pyuv.Async(self.loop, self.async_cb)
     self.signal_h = pyuv.Signal(self.loop)
     self.signal_h.start(self.signal_cb, signal.SIGUSR1)
     thread = threading.Thread(target=self.loop.run)
     thread.start()
     os.kill(os.getpid(), signal.SIGUSR1)
     thread.join()
     self.assertEqual(self.async_cb_called, 1)
     self.assertEqual(self.signal_cb_called, 1)
Ejemplo n.º 16
0
def test_registry_events():
    loop = pyuv.Loop.default_loop()
    async = pyuv.Async(loop, lambda h: h.stop())

    r = Registry(loop)
    emitted = []

    def cb(event, message):
        emitted.append((event, copy.deepcopy(message)))

    r.bind_all(cb)
    c1 = object()
    r.add_node(c1)

    r.identify(c1, "c1", "broadcast", 1.0)
    r.update(c1)
    r.add_job(c1, "a.job1")
    r.add_process(c1, "a.job1", 1)
    r.remove_process(c1, "a.job1", 1)
    r.remove_job(c1, "a.job1")
    r.remove_node(c1)

    t = pyuv.Timer(loop)
    t.start(lambda h: async .close(), 0.2, 0.0)
    loop.run()

    assert len(emitted) == 7
    actions = [line[0] for line in emitted]
    assert list(actions) == [
        'add_node', 'identify', 'add_job', 'add_process', 'remove_process',
        'remove_job', 'remove_node'
    ]

    assert isinstance(emitted[0][1], GafferNode)
    assert isinstance(emitted[1][1], GafferNode)
    assert isinstance(emitted[2][1], dict)
    assert "job_name" in emitted[2][1]
    assert emitted[2][1]['job_name'] == "a.job1"
    assert isinstance(emitted[3][1], dict)
    assert "job_name" in emitted[3][1]
    assert emitted[3][1]['job_name'] == "a.job1"
    assert "pid" in emitted[3][1]
    assert emitted[3][1]['pid'] == 1
    assert isinstance(emitted[4][1], dict)
    assert "job_name" in emitted[4][1]
    assert emitted[4][1]['job_name'] == "a.job1"
    assert "pid" in emitted[4][1]
    assert emitted[4][1]['pid'] == 1
    assert isinstance(emitted[5][1], dict)
    assert emitted[5][1]['job_name'] == "a.job1"
    assert isinstance(emitted[6][1], GafferNode)
    assert emitted[6][1].sessions == {}
Ejemplo n.º 17
0
 def run_in_executor(self, callback):
     # PosixEventLoop creates a thread function to call the callback and
     # gives that to the executor... Apparently prompt_toolkit might rely
     # on this so that it doesn't process autocompletions during paste in a
     # heavy manner. TODO: Revisit this (they have a note about i/o vs cpu
     # preferencing)
     def wrapper(handle):
         handle.close()
         callback()
         i = self.pending_async.index(handle)
         del self.pending_async[i]
     a = pyuv.Async(self.realloop, wrapper)
     self.pending_async.append(a)
     a.send()
Ejemplo n.º 18
0
 def call_from_executor(self, callback, _max_postpone_until=None):
     #TODO: Mess with max postpone? PosixEventLoop uses a pipe to schedule
     # a callback for execution.
     # We'll just call the function via pyuv Async and be done with it.
     def wrapper(handle):
         handle.close()
         callback()
         i = self.pending_async.index(handle)
         del self.pending_async[i]
     a = pyuv.Async(self.realloop, wrapper)
     # If we don't store a somewhere ourselves, libuv never calls the
     # callback. I suspect it is getting garbage collected if we don't keep
     # a reference ourselves.
     self.pending_async.append(a)
     a.send()
Ejemplo n.º 19
0
    def __init__(self):
        super().__init__()
        self._loop = pyuv.Loop()
        self._loop._rose_loop = self
        self._default_executor = None
        self._last_exc = None

        self._fd_map = {}
        self._signal_handlers = {}
        self._subprocesses = {}
        self._ready = collections.deque()
        self._timers = collections.deque()

        self._waker = pyuv.Async(self._loop, self._async_cb)
        self._ready_processor = pyuv.Idle(self._loop)
Ejemplo n.º 20
0
 def __init__(self):
     self._loop = pyuv.Loop()
     self._async_handle = pyuv.Async(self._loop, self._async_cb)
     self._async_handle_lock = threading.Lock()
     self._async_callbacks = deque()
     self._readers = {}  # map of reader objects to fd
     self._writers = {}  # map of writer objects to fd
     self._fds = {}  # map of fd to a (reader, writer) tuple
     self._delayedCalls = {}
     self._poll_handles = {}
     self._signal_fds = SocketPair()
     self._signal_checker = pyuv.util.SignalChecker(
         self._loop, self._signal_fds.reader_fileno())
     self._signal_checker.unref()
     self._signal_checker.start()
     PosixReactorBase.__init__(self)
Ejemplo n.º 21
0
    def start(self, apps=[]):
        """ start the manager. """
        self.mapps = apps

        self._waker = pyuv.Async(self.loop, self._wakeup)

        # start the process tracker
        self._tracker.start()

        # manage processes
        self.events.subscribe('exit', self._on_exit)

        # start contollers
        for mapp in self.mapps:
            mapp.start(self.loop, self)

        self.started = True
        self.status = 0
Ejemplo n.º 22
0
    def test_async1(self):
        self.async_cb_called = 0
        self.close_cb_called = 0

        def close_cb(handle):
            self.close_cb_called += 1

        def async_cb(async):
            self.async_cb_called += 1
            async .close(close_cb)

        loop = pyuv.Loop.default_loop()
        thread = threading.Thread(target=loop.run)
        async = pyuv.Async(loop)
        thread.start()
        async .send(async_cb)
        thread.join()
        self.assertEqual(self.async_cb_called, 1)
        self.assertEqual(self.close_cb_called, 1)
Ejemplo n.º 23
0
    def test_embed(self):
        if poller is None:
            self.skipTest("test disabled if no suitable poller method is found")
            return
        self.embed_timer_called = 0
        self.embed_closed = False
        self.external = pyuv.Loop()
        self.embed_async = pyuv.Async(self.external, self.embed_cb)

        self.loop = pyuv.Loop()
        timer = pyuv.Timer(self.loop)
        timer.start(self.timer_cb, 0.25, 0)

        self.sem = pyuv.thread.Semaphore(0)
        t = Thread(target=self.embed_runner)
        t.start()
        self.external.run()
        t.join()
        external = None

        self.assertEqual(self.embed_timer_called, 1)
Ejemplo n.º 24
0
 def __init__(self, loop, ref=True):
     super(Async, self).__init__(loop, ref)
     self._handle = pyuv.Async(self.loop._loop, self._async_cb)
Ejemplo n.º 25
0

def async_exit(async):
    async .close()
    signal_h.close()
    server.close()


def signal_cb(sig, frame):
    async .send(async_exit)


print("PyUV version %s" % pyuv.__version__)

loop = pyuv.Loop()
async = pyuv.Async(loop)

server = pyuv.UDP(loop)
server.bind(("0.0.0.0", 1234))
server.start_recv(on_read)

signal_h = pyuv.Signal(loop)
signal_h.start()

t = threading.Thread(target=loop.run)
t.start()

signal.signal(signal.SIGINT, signal_cb)
signal.pause()

t.join()
Ejemplo n.º 26
0
 def start_process(self):
     self. async = pyuv.Async(self._loop, self._start_process_async)
     self. async .send()
Ejemplo n.º 27
0
 def __init__(self, reactor):
     self._async = pyuv.Async(reactor._loop, lambda x: None)
     self._async.unref()
Ejemplo n.º 28
0
    def __init__(self, icommand, order_queue, event_queue):
        try:
            self.order_queue = order_queue  # Messages from master thread
            self.event_queue = event_queue  # Messages to master thread

            self.master_fd, self.slave_fd = pty.openpty()

            pty_make_controlling_tty(self.slave_fd)

            self.stream = pyte.Stream()
            self.screen = pyte.Screen(icommand.width, icommand.height)
            self.stream.attach(self.screen)
            self.raw_byte_output = b''
            self.timeout = icommand.timeout
            self.task = None

            self.loop = pyuv.Loop.default_loop()

            self.tty_handle = pyuv.TTY(self.loop, self.master_fd, True)
            self.tty_handle.start_read(self.on_tty_read)

            self.sigterm_handle = pyuv.Signal(self.loop)
            self.sigterm_handle.start(self.sigterm_callback, signal.SIGTERM)
            self.sigint_handle = pyuv.Signal(self.loop)
            self.sigint_handle.start(self.sigint_callback, signal.SIGINT)

            self.process_handle = pyuv.Process.spawn(
                self.loop,
                args=icommand._command.arguments,
                env=icommand._command.env,
                cwd=icommand._command.directory,
                exit_callback=self.on_exit,
                stdio=[
                    pyuv.StdIO(
                        fd=self.slave_fd,
                        flags=pyuv.UV_INHERIT_FD,
                    ),
                    pyuv.StdIO(
                        fd=self.slave_fd,
                        flags=pyuv.UV_INHERIT_FD,
                    ),
                    pyuv.StdIO(
                        fd=self.slave_fd,
                        flags=pyuv.UV_INHERIT_FD,
                    ),
                ],
                flags=pyuv.UV_PROCESS_DETACHED,
            )

            self._pid = self.process_handle.pid

            self.event_queue.put(
                message.ProcessStartedMessage(
                    message.RunningProcess(self._pid, self.master_fd)))

            self.async_handle = pyuv.Async(self.loop, self.on_thread_callback)
            self.event_queue.put(
                message.AsyncSendMethodMessage(self.async_handle.send))

            self.timeout_handle = None
            self.reset_timeout()

            self.loop.run()
        except Exception as error:
            self.close_handles()
            self.event_queue.put(message.ExceptionMessage(error))
Ejemplo n.º 29
0
def async_exit(async):
    [c.close() for c in clients]
    async .close()
    signal_h.close()
    server.close()


def signal_cb(handle, signum):
    global async
    async .send()


print("PyUV version %s" % pyuv.__version__)

loop = pyuv.Loop.default_loop()

async = pyuv.Async(loop, async_exit)
clients = []

server = pyuv.TCP(loop)
server.bind(("0.0.0.0", 1234))
server.listen(on_connection)

signal_h = pyuv.Signal(loop)
signal_h.start(signal_cb, signal.SIGINT)

loop.run()

print("Stopped!")
Ejemplo n.º 30
0
 def __init__(self, loop):
     self._async = pyuv.Async(loop, lambda x: None)