Example #1
0
 def test_io(self):
     if sys.platform == 'win32':
         Error = IOError
     else:
         Error = ValueError
     self.assertRaises(Error, core.loop().io, -1, 1)
     self.assertRaises(ValueError, core.loop().io, 1, core.TIMER)
Example #2
0
    def test_io(self):
        if sys.platform == 'win32':
            # libev raises IOError, libuv raises ValueError
            Error = (IOError, ValueError)
            win32 = True
        else:
            Error = ValueError
            win32 = False
        with self.assertRaises(Error):
            core.loop().io(-1, 1)
        if hasattr(core, 'TIMER'):
            # libev
            with self.assertRaises(ValueError):
                core.loop().io(1, core.TIMER)

        # Test we can set events and io before it's started
        if not win32:
            # We can't do this with arbitrary FDs on windows;
            # see libev_vfd.h
            io = core.loop().io(1, core.READ)
            io.fd = 2
            self.assertEqual(io.fd, 2)
            io.events = core.WRITE
            if not hasattr(core, 'libuv'):
                # libev
                self.assertEqual(core._events_to_str(io.events),
                                 'WRITE|_IOFDSET')
            else:
                self.assertEqual(core._events_to_str(io.events), 'WRITE')
            io.close()
Example #3
0
 def test_io(self):
     if sys.platform == 'win32':
         Error = IOError
     else:
         Error = ValueError
     self.assertRaises(Error, core.loop().io, -1, 1)
     self.assertRaises(ValueError, core.loop().io, 1, core.TIMER)
Example #4
0
 def test_flags_conversion(self):
     if sys.platform != 'win32':
         self.assertEqual(core.loop(2, default=False).backend_int, 2)
     self.assertEqual(core.loop('select', default=False).backend, 'select')
     self.assertEqual(core._flags_to_int(None), 0)
     self.assertEqual(core._flags_to_int(['kqueue', 'SELECT']), core.BACKEND_KQUEUE | core.BACKEND_SELECT)
     self.assertEqual(core._flags_to_list(core.BACKEND_PORT | core.BACKEND_POLL), ['port', 'poll'])
     self.assertRaises(ValueError, core.loop, ['port', 'blabla'])
     self.assertRaises(TypeError, core.loop, object())
Example #5
0
 def test_flags_conversion(self):
     # pylint: disable=no-member
     if not greentest.WIN:
         self.assertEqual(core.loop(2, default=False).backend_int, 2)
     self.assertEqual(core.loop('select', default=False).backend, 'select')
     self.assertEqual(core._flags_to_int(None), 0)
     self.assertEqual(core._flags_to_int(['kqueue', 'SELECT']),
                      core.BACKEND_KQUEUE | core.BACKEND_SELECT)
     self.assertEqual(
         core._flags_to_list(core.BACKEND_PORT | core.BACKEND_POLL),
         ['port', 'poll'])
     self.assertRaises(ValueError, core.loop, ['port', 'blabla'])
     self.assertRaises(TypeError, core.loop, object())
Example #6
0
    def run(self):
        """
        注册master和branch协程
        启动loop,监听接入层请求,监听旁路回包
        接入层入口 self.master_watch
        旁路入口  self.branch_watch
        :return: 注册失败则程序推出
        """
        l = loop()

        # 注册master处理方法
        try:
            io = l.io(self.master_sock.fileno(), 1)  # 1代表read
            io.start(self.master_watch)
        except Exception as e:
            g_log.critical("error: %s", e)
            sys.exit(-1)
        g_log.debug("register master coroutine self.master_watch done ...")

        # 注册旁路处理方法
        for handler in self.branch_handler:
            try:
                sock = handler["sock"]
                io = l.io(sock.fileno(), 1)
                io.start(self.branch_watch(handler["name"]))
            except Exception as e:
                g_log.critical("error:", e)
                sys.exit(-1)
            g_log.debug("register branch %s coroutine self.branch_watch done ...", handler["name"])
        g_log.debug("register branch coroutine done ...")

        # 启动libev loop
        g_log.debug("now start event loop...")
        l.run()
Example #7
0
    def test_invalid_fd(self):
        loop = core.loop(default=False)

        # Negative case caught everywhere. ValueError
        # on POSIX, OSError on Windows Py3, IOError on Windows Py2
        with self.assertRaises((ValueError, OSError, IOError)):
            loop.io(-1, core.READ)

        loop.destroy()
Example #8
0
 def test_io(self):
     if sys.platform == 'win32':
         Error = IOError
         win32 = True
     else:
         Error = ValueError
         win32 = False
     self.assertRaises(Error, core.loop().io, -1, 1)
     self.assertRaises(ValueError, core.loop().io, 1, core.TIMER)
     # Test we can set events and io before it's started
     if not win32:
         # We can't do this with arbitrary FDs on windows;
         # see libev_vfd.h
         io = core.loop().io(1, core.READ)
         io.fd = 2
         self.assertEqual(io.fd, 2)
         io.events = core.WRITE
         self.assertEqual(core._events_to_str(io.events), 'WRITE|_IOFDSET')
    def test_types(self):
        loop = core.loop()
        lst = []

        io = loop.timer(0.01)

        # test that cannot pass non-callable thing to start()
        self.assertRaises(TypeError, io.start, None)
        self.assertRaises(TypeError, io.start, 5)
        # test that cannot set 'callback' to non-callable thing later either
        io.start(lambda *args: lst.append(args))
        self.assertEqual(io.args, ())
        try:
            io.callback = False
            raise AssertionError('"io.callback = False" must raise TypeError')
        except TypeError:
            pass
        try:
            io.callback = 5
            raise AssertionError('"io.callback = 5" must raise TypeError')
        except TypeError:
            pass
        # test that args can be changed later
        io.args = (1, 2, 3)
        if greentest.PYPY:
            pass  # on PYPY .args is just a normal property
        else:
            # test that only tuple and None are accepted by 'args' attribute
            try:
                io.args = 5
                raise AssertionError('"io.args = 5" must raise TypeError')
            except TypeError:
                pass
            self.assertEqual(io.args, (1, 2, 3))
            try:
                io.args = [4, 5]
                raise AssertionError('"io.args = [4, 5]" must raise TypeError')
            except TypeError:
                pass
        self.assertEqual(io.args, (1, 2, 3))
        if greentest.PYPY:
            io.args = ()
        else:
            # None also works, means empty tuple
            # XXX why?
            io.args = None
        start = core.time()
        loop.run()
        took = core.time() - start
        self.assertEqual(lst, [()])
        assert took < 1, took

        io.start(reset, io, lst)
        del io
        loop.run()
        self.assertEqual(lst, [(), 25])
Example #10
0
    def test_types(self):
        loop = core.loop(default=False)
        lst = []

        io = loop.timer(0.01)

        # test that cannot pass non-callable thing to start()
        self.assertRaises(TypeError, io.start, None)
        self.assertRaises(TypeError, io.start, 5)
        # test that cannot set 'callback' to non-callable thing later either
        io.start(lambda *args: lst.append(args))
        self.assertEqual(io.args, ())
        try:
            io.callback = False
            raise AssertionError('"io.callback = False" must raise TypeError')
        except TypeError:
            pass
        try:
            io.callback = 5
            raise AssertionError('"io.callback = 5" must raise TypeError')
        except TypeError:
            pass
        # test that args can be changed later
        io.args = (1, 2, 3)
        # test that only tuple and None are accepted by 'args' attribute
        self.assertRaises(TypeError, setattr, io, 'args', 5)
        self.assertEqual(io.args, (1, 2, 3))

        self.assertRaises(TypeError, setattr, io, 'args', [4, 5])
        self.assertEqual(io.args, (1, 2, 3))
        # None also works, means empty tuple
        # XXX why?
        io.args = None
        self.assertEqual(io.args, None)
        time_f = getattr(core, 'time', loop.now)
        start = time_f()
        loop.run()
        took = time_f() - start
        self.assertEqual(lst, [()])
        if hasattr(core, 'time'):
            # only useful on libev
            assert took < 1, took

        io.start(reset, io, lst)
        del io
        loop.run()
        self.assertEqual(lst, [(), 25])
        loop.destroy()
Example #11
0
    def test_types(self):
        loop = core.loop()
        lst = []

        io = loop.timer(0.01)

        # test that cannot pass non-callable thing to start()
        self.assertRaises(TypeError, io.start, None)
        self.assertRaises(TypeError, io.start, 5)
        # test that cannot set 'callback' to non-callable thing later either
        io.start(lambda *args: lst.append(args))
        self.assertEqual(io.args, ())
        try:
            io.callback = None
            raise AssertionError('"io.callback = None" must raise TypeError')
        except TypeError:
            pass
        try:
            io.callback = 5
            raise AssertionError('"io.callback = 5" must raise TypeError')
        except TypeError:
            pass
        # test that args can be changed later
        io.args = (1, 2, 3)
        # test that only tuple and None are accepted by 'args' attribute
        try:
            io.args = 5
            raise AssertionError('"io.args = 5" must raise TypeError')
        except TypeError:
            pass
        self.assertEqual(io.args, (1, 2, 3))
        try:
            io.args = [4, 5]
            raise AssertionError('"io.args = [4, 5]" must raise TypeError')
        except TypeError:
            pass
        self.assertEqual(io.args, (1, 2, 3))
        # None also works, means empty tuple
        io.args = None
        loop.run()
        self.assertEqual(lst, [()])

        io.start(reset, io, lst)
        del io
        loop.run()
        self.assertEqual(lst, [(), 25])
Example #12
0
    def test_reuse_io(self):
        loop = core.loop(default=False)

        # Watchers aren't reused once all outstanding
        # refs go away
        tty_watcher = loop.io(1, core.WRITE)
        watcher_handle = tty_watcher._watcher if IS_CFFI else tty_watcher

        del tty_watcher
        # XXX: Note there is a cycle in the CFFI code
        # from watcher_handle._handle -> watcher_handle.
        # So it doesn't go away until a GC runs.
        import gc
        gc.collect()

        tty_watcher = loop.io(1, core.WRITE)
        self.assertIsNot(tty_watcher._watcher if IS_CFFI else tty_watcher,
                         watcher_handle)

        loop.destroy()
Example #13
0
def main():
    loop = core.loop(default=True)
    x = loop.timer(0.001)
    x.start(f)

    assert x.active, x.pending
    try:
        x.priority = 1
        raise AssertionError('must not be able to change priority of active watcher')
    except AttributeError:
        pass
    loop.run()
    assert x.pending == 0, x.pending
    assert called == [1], called
    assert x.callback is None, x.callback
    assert x.args is None, x.args
    assert x.priority == 0, x
    x.priority = 1
    assert x.priority == 1, x
    x.stop()
Example #14
0
def main():
    loop = core.loop(default=True)
    x = loop.timer(0.001)
    x.start(f)
    if hasattr(loop, '_keepaliveset'):
        assert x in loop._keepaliveset
    assert x.active, ("active", x.active, "pending", x.pending)
    try:
        x.priority = 1
        raise AssertionError(
            'must not be able to change priority of active watcher')
    except (AttributeError, ValueError):
        pass
    loop.run()
    assert x.pending == 0, x.pending
    assert called == [1], called
    assert x.callback is None, x.callback
    assert x.args is None, x.args
    if x.priority is not None:
        assert x.priority == 0, (x, x.priority)
        x.priority = 1
        assert x.priority == 1, x
    x.stop()
    if hasattr(loop, '_keepaliveset'):
        assert x not in loop._keepaliveset

    # Again works for a new timer
    x = loop.timer(0.001, repeat=1)
    x.again(f, x)
    if hasattr(loop, '_keepaliveset'):
        assert x in loop._keepaliveset

    assert x.args == (x, ), x.args
    loop.run()
    assert called == [1, 1], called

    x.stop()
    if hasattr(loop, '_keepaliveset'):
        assert x not in loop._keepaliveset
Example #15
0
def main():
    loop = core.loop(default=True)
    x = loop.timer(0.001)
    x.start(f)
    if hasattr(loop, '_keepaliveset'):
        assert x in loop._keepaliveset
    assert x.active, x.pending
    try:
        x.priority = 1
        raise AssertionError('must not be able to change priority of active watcher')
    except AttributeError:
        pass
    loop.run()
    assert x.pending == 0, x.pending
    assert called == [1], called
    assert x.callback is None, x.callback
    assert x.args is None, x.args
    assert x.priority == 0, x
    x.priority = 1
    assert x.priority == 1, x
    x.stop()
    if hasattr(loop, '_keepaliveset'):
        assert x not in loop._keepaliveset

    # Again works for a new timer
    x = loop.timer(0.001, repeat=1)
    x.again(f, x)
    if hasattr(loop, '_keepaliveset'):
        assert x in loop._keepaliveset

    assert x.args == (x,), x.args
    loop.run()
    assert called == [1, 1], called

    x.stop()
    if hasattr(loop, '_keepaliveset'):
        assert x not in loop._keepaliveset
Example #16
0
 def test_flags_conversion(self):
     if sys.platform != 'win32':
         self.assertEqual(core.loop(2, default=False).backend_int, 2)
     self.assertEqual(core.loop('select', default=False).backend, 'select')
     self.assertEqual(core._flags_to_int(None), 0)
     self.assertEqual(core._flags_to_int(['kqueue', 'SELECT']), core.BACKEND_KQUEUE | core.BACKEND_SELECT)
     self.assertEqual(core._flags_to_list(core.BACKEND_PORT | core.BACKEND_POLL), ['port', 'poll'])
     try:
         core.loop(['port', 'blabla'])
     except ValueError as ex:
         if sys.version_info[0] > 2:
             ex.__traceback__ = None
     else:
         raise AssertionError("ValueError is not raised")
     try:
         core.loop(object())
     except TypeError as ex:
         if sys.version_info[0] > 2:
             ex.__traceback__ = None
     else:
         raise AssertionError("TypeError is not raised")
Example #17
0
 def test_flags_conversion(self):
     if sys.platform != 'win32':
         self.assertEqual(core.loop(2, default=False).backend_int, 2)
     self.assertEqual(core.loop('select', default=False).backend, 'select')
     self.assertEqual(core._flags_to_int(None), 0)
     self.assertEqual(core._flags_to_int(['kqueue', 'SELECT']),
                      core.BACKEND_KQUEUE | core.BACKEND_SELECT)
     self.assertEqual(
         core._flags_to_list(core.BACKEND_PORT | core.BACKEND_POLL),
         ['port', 'poll'])
     try:
         core.loop(['port', 'blabla'])
     except ValueError as ex:
         if sys.version_info[0] > 2:
             ex.__traceback__ = None
     else:
         raise AssertionError("ValueError is not raised")
     try:
         core.loop(object())
     except TypeError as ex:
         if sys.version_info[0] > 2:
             ex.__traceback__ = None
     else:
         raise AssertionError("TypeError is not raised")
Example #18
0
# test for issue #210
from gevent import core
from gevent.testing.util import alarm


alarm(1)

log = []
loop = core.loop(default=False)
loop.run_callback(log.append, 1)
loop.run()
assert log == [1], log
Example #19
0
import sys
from gevent import core, signal
loop = core.loop()


signal = signal(2, sys.stderr.write, 'INTERRUPT!')

print ('must exit immediatelly...')
loop.run()  # must exit immediatelly
print ('...and once more...')
loop.run()  # repeating does not fail
print ('..done')

print ('must exit after 0.5 seconds.')
timer = loop.timer(0.5)
timer.start(lambda: None)
loop.run()

del loop
Example #20
0
 def test_signal(self):
     self.assertRaises(ValueError, core.loop().signal, 1000)
Example #21
0
 def test_timer(self):
     self.assertRaises(ValueError, core.loop().timer, 1, -1)
Example #22
0
    for r in wl:
        r.send('hello world\r\n')
        r.close()
        wlist.remove(r)

4.gevent loop使用
import socket
import gevent
from gevent.core import loop

def f():
    s, address = sock.accept()
    print address
    s.send("hello world\r\n")

loop = loop()
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind(("localhost",8000))
sock.listen(10)
io = loop.io(sock.fileno(),1) #1代表read
io.start(f)
loop.run()

5.gevent recv流程
    
    gevent的socket对象被gevent重新封装,原始socket就是下面的self._sock
我们来看看gevent的socket一次recv做了什么操作。
    def recv(self, *args):
        sock = self._sock  # keeping the reference so that fd is not closed during waiting
        while True:
            try:
Example #23
0
 def test_timer_constructor(self):
     with self.assertRaises(ValueError):
         core.loop().timer(1, -1)
Example #24
0
 def _makeOne(self):
     # pylint: disable=no-member
     l = core.loop(default=True)
     l.destroy()
     del l
     return core.loop(default=True)
Example #25
0
from __future__ import print_function
import sys
from gevent import core
from gevent import signal_handler as signal
loop = core.loop(default=False)

signal = signal(2, sys.stderr.write, 'INTERRUPT!')

print('must exit immediately...')
loop.run()  # must exit immediately
print('...and once more...')
loop.run()  # repeating does not fail
print('..done')

print('must exit after 0.5 seconds.')
timer = loop.timer(0.5)
timer.start(lambda: None)
loop.run()
timer.close()
loop.destroy()
del loop
Example #26
0
 def test_timer(self):
     self.assertRaises(ValueError, core.loop().timer, 1, -1)
Example #27
0
 def setUp(self):
     self.called = []
     self.loop = core.loop(default=True)
     self.timer = self.loop.timer(0.001, repeat=self.repeat)
Example #28
0
 def _makeOne(self):
     return core.loop()  # pylint:disable=no-member
Example #29
0
 def __init__(self, connection):
     self._conn = connection
     self._buf = ""
     self.watcher = loop(connection.fileno(), 1)
     self.watcher.start(self.handle)
Example #30
0
 def _makeOne(self):
     return core.loop(default=True)  # pylint:disable=no-member
Example #31
0
from gevent.core import loop

count = 0

def incr():
    global count
    count += 1

loop = loop()
loop.callback().start(incr)
loop.run()
assert count == 1, count
Example #32
0
 def test_signal(self):
     self.assertRaises(ValueError, core.loop().signal, 1000)
Example #33
0
 def test_signal_constructor(self):
     with self.assertRaises(ValueError):
         core.loop().signal(1000)
from gevent.core import loop

count = 0


def incr():
    global count
    count += 1


loop = loop()
loop.run_callback(incr)
loop.run()
assert count == 1, count
Example #35
0
from __future__ import print_function
import sys
from gevent import core, signal
loop = core.loop()

signal = signal(2, sys.stderr.write, 'INTERRUPT!')

print('must exit immediatelly...')
loop.run()  # must exit immediatelly
print('...and once more...')
loop.run()  # repeating does not fail
print('..done')

print('must exit after 0.5 seconds.')
timer = loop.timer(0.5)
timer.start(lambda: None)
loop.run()

del loop