Пример #1
0
class AnotherReactor(threading.Thread):
    def __init__(self, port, type):
        threading.Thread.__init__(self)
        self._acceptor = Acceptor(HttpConnection)
        self._reactor = Reactor(select_type = type)
        if not self._reactor.open():
            print("reactor open failed")
        self._acceptor.open(("127.0.0.1", port), self._reactor)

    def run(self):
        while True:
            if not self._reactor.handle_events():
                break
        print("another reactor exit")
Пример #2
0
 def __init__(self, port, type):
     threading.Thread.__init__(self)
     self._acceptor = Acceptor(HttpConnection)
     self._reactor = Reactor(select_type = type)
     if not self._reactor.open():
         print("reactor open failed")
     self._acceptor.open(("127.0.0.1", port), self._reactor)
Пример #3
0
                self._send_bytes += self._sock.send(self._buffer[self._send_bytes:])
                if self._send_bytes == len(self._buffer):
                    break
            except socket.error as e:
                if e[0] == errno.EWOULDBLOCK:
                    return True
                print(e)
                break
        self._buffer = None
        self._send_bytes = 0
        return False

    def handle_close(self, handle, mask):
        print("handle close")
        super(Echo, self).handle_close(handle, mask)

if __name__ == '__main__':
    g_reactor = Reactor()
    if not g_reactor.open():
        print("reactor open failed")
    
    if sys.argv[1] == 'server':
        g_acceptor = Acceptor(Echo)
        g_acceptor.open(("127.0.0.1", 8008), g_reactor)
    elif sys.argv[1] == 'client':
        g_connector = Connector()
        g_connector.open(g_reactor)
        g_connector.connect(Echo('client'), ("127.0.0.1", 8008), 5)

    g_reactor.reactor_event_loop()
Пример #4
0
            return False
        if self._count > 5 and arg[1] == 'reset' and not self._reset:
            if g_reactor.reset_timer_interval(self, 6):
                self._reset = True
                print("%s reset interval to %d" % (arg[0], 6))
        if self._count > 5 and arg[1] == 'cancel':
            if g_reactor.cancel_timer(self, False):
                print("%s canceled" % arg[0])
        return True

    def handle_close(self, arg, flag):
        print("%s closed" % arg[0])

if __name__ == '__main__':

    g_reactor = Reactor(thread_safe = False)
    if not g_reactor.open():
        print("reactor open failed")

    timerid = g_reactor.schedule_timer(TimerTest(), ('loop-20-0.2', 'loop'), 20, 0.2)
    if timerid < 0:
        print("schedule timer failed") 
    else:
        print("schedule %s in [%s] ok" % ('loop-20-0.2', datetime.now()))

    timerid = g_reactor.schedule_timer(TimerTest(), ('loop-4.5-2', 'loop'), 4.5, 2)
    if timerid < 0:
        print("schedule timer failed") 
    else:
        print("schedule %s in [%s] ok" % ('loop-4.5-2', datetime.now()))
Пример #5
0
from datetime import datetime

class Crontab(EventHandler):
    def __init__(self):
        EventHandler.__init__(self)

    def handle_timeout(self, current_time, arg):
        print("%s called in %s" % (arg, current_time))
        return True

    def handle_close(self, arg, flag):
        print("%s closed" % arg)

if __name__ == '__main__':

    g_reactor = Reactor()
    if not g_reactor.open():
        print("reactor open failed")

    # crontab
    entry = "2-10 2-10 23-7/2 * mon-/2"
    print("%s" % entry)
    print("%s timerid = %d" % ('=' * 30, g_reactor.crontab(Crontab(), entry, entry)))

    entry = "45-10/2 * 23-7/2 * mon-/2"
    print("%s" % entry)
    print("%s timerid = %d" % ('=' * 30, g_reactor.crontab(Crontab(), entry, entry)))

    entry = "12/2 */2 23-30/2 4-10/2,11/2,12- 2,4,7"
    print("%s" % entry)
    print("%s timerid = %d" % ('=' * 30, g_reactor.crontab(Crontab(), entry, entry)))
Пример #6
0
        try: fileid = g_filelist.pop(0)
        except: break
        #url = php_interface + "?url=" + fileid
        url = php_interface + "?" + fileid
        addr = parse_addr(php_interface)
        con = PHPInterface(fileid, addr, url)
        g_connector.connect(con, addr, 4)
        g_active_task_num += 1

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print("check_push.py phpinterface filelist recvbytes(default is filesize)")
        sys.exit(1)
    
    php_interface = sys.argv[1]
    g_reactor = Reactor("any", False)
    if not g_reactor.open():
        print("reactor open failed")

    for line in open(sys.argv[2], "rt"):
        s = line.strip()
        if len(s) > 0: g_filelist.append(s);
    if len(sys.argv) == 4:
        g_recv_bytes = int(sys.argv[3])
    g_task_num = len(g_filelist)
    g_connector = Connector()
    g_connector.open(g_reactor)
    dispatch_task()
    while True:
        if not g_reactor.handle_events():
            print("reactor error")