Ejemplo n.º 1
0
 def test_handles(self):
     timer = pyuv.Timer(self.loop)
     self.assertTrue(timer in self.loop.handles)
     timer = None
     self.loop.run()
     self.assertFalse(self.loop.handles)
Ejemplo n.º 2
0
 def __init__(self, loop):
     self.processes = []
     self._done_cb = None
     self._check_timer = pyuv.Timer(loop)
     self._lock = RLock()
Ejemplo n.º 3
0
import pyuv
import signal

l = pyuv.Loop()
t = pyuv.Timer(l)
global remain
remain = 4


def echo(arg):
    print "echo"
    global remain
    remain -= 1
    if remain <= 0:
        arg.stop()


s = pyuv.Signal(l)


def sighand(sighand, signum):
    print
    print "signum", signum, "received. Exiting"
    sighand.loop.stop()


s.start(sighand, signal.SIGINT)
t.start(echo, 0, 2)
l.run()
print "Post event loop"
Ejemplo n.º 4
0

def send_registration():
    register_payload = struct.pack('!IHIB' + str(len(SERVICE_NAME)) + 's',
                                   LOCAL_IP, SERVICE_PORT, SERVICE_DATA,
                                   len(SERVICE_NAME), SERVICE_NAME)
    send(Packet461(next_seq_num(), ACTION_REGISTER, register_payload))


#---------------------------------------------------------------------
# mainline
#---------------------------------------------------------------------

loop = pyuv.Loop.default_loop()

reregistration_timer = pyuv.Timer(loop)
rexmit_timer = pyuv.Timer(loop)
rexmit_timer.start(rexmit, SERVER_RESPONSE_TIMEOUT, SERVER_RESPONSE_TIMEOUT)

# Make sure we clean up and exit on Ctrl-C
signal_h = pyuv.Signal(loop)
signal_h.start(shutdown, signal.SIGINT)

#sending_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sending_socket = pyuv.UDP(loop)
sending_socket.start_recv(on_read)

# Kick things off by registering with the server
send_registration()

#print "[registration client] Sent registration!"
Ejemplo n.º 5
0
 def _start_external(self):
     self._external_terminal = pyuv.Timer(self._loop)
     self._external_terminal.start(self._read_external_terminal(), 0, 0.001)
Ejemplo n.º 6
0
        print error
        sys.exit(0)
    # raw, address = sending_socket.recvfrom(1024)
    packet = Packet461.fromstring(raw)
    if packet and packet.action == ACTION_FETCH_RESPONSE:
        (packet.numFetchEntries, ) = struct.unpack("!B", packet.payload[0])
        # print str(packet.numFetchEntries) + " entries"
        offset = 1
        for e in xrange(packet.numFetchEntries):
            entry = Packet461.parseFetchResponseEntry(
                packet.payload[offset:offset + 10])
            print str(entry['ip']) + "\t" + str(entry['port']) + "\t" + str(
                entry['data'])
            offset += 10
        global loop
        loop.stop()


loop = pyuv.Loop.default_loop()

sending_socket = pyuv.UDP(loop)
sending_socket.start_recv(on_read)

payload = struct.pack('!B' + str(len(fetchKey)) + 's', len(fetchKey), fetchKey)
fetch_packet = Packet461(ACTION_FETCH, payload)

rexmit_timer = pyuv.Timer(loop)
rexmit_timer.start(send, 0, SERVER_RESPONSE_TIMEOUT)

loop.run()
Ejemplo n.º 7
0
server = pyuv.TCP(loop)
server.bind(('127.0.0.1', port))
server.listen(on_connection)


def on_timer(timer):
    timer.stop()
    port = random.choice(ports)

    print 'sending hop command to port %s' % port

    def on_connected(handle, error):
        handle.write('hop hop\n', on_written)

    def on_written(handle, error):
        handle.close()
        fanout_client.disconnect()
        server.close()

    handle = pyuv.TCP(timer.loop)
    handle.connect(('127.0.0.1', int(port)), on_connected)


timer = pyuv.Timer(loop)
timer.start(on_timer, 2, 0)

fanout_client = Hopper(('127.0.0.1', 9898), port)
fanout_client.connect()

loop.run()
Ejemplo n.º 8
0
 def __init__(self, loop):
     self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb)
     self.loop = loop
     self._timer = pyuv.Timer(self.loop)
     self._fd_map = {}
Ejemplo n.º 9
0
 def __init__(self, callback, callback_time, io_loop=None):
     self.callback = callback
     self.callback_time = callback_time / 1000.0
     self.io_loop = io_loop or IOLoop.instance()
     self._timer = pyuv.Timer(self.io_loop._loop)
     self._running = False
Ejemplo n.º 10
0
    #cli.reset()
    #cli.renderer.erase()
    #print cli.renderer._cursor_pos.x


def loopstop(event):
    event.loop.stop()
    # If we close here, and we close after the run (t2.close() below), we get
    # an error from libuv. Since we know we'll close below, don't bother
    # closing here.
    #event.close()


s = pyuv.Signal(uvloop)
s.start(sigint, signal.SIGINT)
t = pyuv.Timer(uvloop)
t.start(timevent, 1, 5)

cli = prompt_toolkit.interface.CommandLineInterface(
    application=prompt_toolkit.shortcuts.create_prompt_application(
        "prompt1> "),  # Other kwargs after message
    eventloop=loop,
    output=prompt_toolkit.shortcuts.create_output(true_color=False),
)

try:
    #res = prompt_toolkit.prompt("prompt> ", eventloop=loop)
    res = cli.run()
    print "res=", repr(res)
except KeyboardInterrupt:
    print 'interrupted'
Ejemplo n.º 11
0
def test_lookup_client():
    # intiallize the lookupd server
    loop = pyuv.Loop.default_loop()
    r = Registry(loop)
    sock = bind_sockets(LOOKUPD_ADDR)
    io_loop = IOLoop(_loop=loop)
    server = http_server(io_loop, sock, registration_db=r)
    server.start()

    lookup_address = "http://%s" % LOOKUPD_ADDR
    client = LookupServer(lookup_address, loop)

    # start the manager with the HTTP API
    http_handler = HttpHandler(
        MockConfig(bind=GAFFERD_ADDR, lookupd_addresses=[lookup_address]))
    m = Manager(loop=loop)
    m.start(apps=[http_handler])
    time.sleep(0.1)
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)

    t = pyuv.Timer(loop)

    def do_stop(h):
        server.stop()
        io_loop.close(True)

    def stop_server(m):
        t.start(do_stop, 0.1, 0.0)

    results = []

    def collect_info2(h):
        results.append(client.sessions())
        results.append(client.jobs())
        m.stop(stop_server)

    def collect_info(h):
        results.append(client.nodes())
        results.append(client.sessions())
        results.append(client.jobs())
        results.append(client.find_job("default.dummy"))
        m.unload("dummy")
        h.start(collect_info2, 0.3, 0.0)

    t0 = pyuv.Timer(loop)
    t0.start(collect_info, 0.3, 0.0)

    loop.run()
    assert len(results) == 6

    host = hostname()

    assert "nodes" in results[0]
    nodes = results[0]['nodes']
    assert len(nodes) == 1

    assert "name" in nodes[0]
    assert nodes[0]['name'] == host

    assert "sessions" in results[1]
    assert "nb_sessions" in results[1]
    assert results[1]["nb_sessions"] == 1
    sessions = results[1]["sessions"]
    assert isinstance(sessions, list)
    assert len(sessions) == 1
    session = sessions[0]
    assert session['sessionid'] == "default"
    assert "jobs" in session
    jobs = session["jobs"]
    assert isinstance(jobs, dict)
    assert "default.dummy" in jobs
    job = jobs["default.dummy"]
    assert isinstance(job, list)
    assert job[0]["name"] == host
    assert job[0]["pids"] == [1]
    node_info = job[0]["node_info"]
    assert node_info["name"] == host

    assert "jobs" in results[2]
    assert "nb_jobs" in results[2]
    assert results[2]["nb_jobs"] == 1
    job1 = results[2]["jobs"][0]
    job1["name"] == "default.dummy"
    assert len(job1["sources"]) == 1
    assert job1["sources"] == job

    assert "sources" in results[3]
    assert len(results[3]["sources"]) == 1
    assert results[3]["sources"] == job

    assert results[4]["sessions"] == []
    assert results[4]["nb_sessions"] == 0

    assert results[5]["jobs"] == []
    assert results[5]["nb_jobs"] == 0
Ejemplo n.º 12
0
def test_lookup_manager():

    # intiallize the lookupd server
    loop = pyuv.Loop.default_loop()
    r = Registry(loop)
    sock = bind_sockets(LOOKUPD_ADDR)
    io_loop = IOLoop(_loop=loop)
    server = http_server(io_loop, sock, registration_db=r)
    server.start()

    # subscribe to events
    emitted = []

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

    r.bind_all(cb)

    # start the manager with the HTTP API
    http_handler = HttpHandler(
        MockConfig(bind=GAFFERD_ADDR,
                   lookupd_addresses=["http://%s" % LOOKUPD_ADDR]))
    m = Manager(loop=loop)
    m.start(apps=[http_handler])

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    m.stop_process(1)
    m.unload("dummy")

    t = pyuv.Timer(loop)

    def do_stop(h):
        server.stop()
        io_loop.close(True)

    def stop_server(m):
        t.start(do_stop, 0.4, 0.0)

    m.stop(stop_server)
    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'] == "default.dummy"
    assert isinstance(emitted[3][1], dict)
    assert "job_name" in emitted[3][1]
    assert emitted[3][1]['job_name'] == "default.dummy"
    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'] == "default.dummy"
    assert "pid" in emitted[4][1]
    assert emitted[4][1]['pid'] == 1
    assert isinstance(emitted[5][1], dict)
    assert emitted[5][1]['job_name'] == "default.dummy"
    assert isinstance(emitted[6][1], GafferNode)
    assert emitted[6][1].sessions == {}
Ejemplo n.º 13
0
def test_lookup_service():
    loop = pyuv.Loop.default_loop()
    r = Registry(loop)
    sock = bind_sockets(LOOKUPD_ADDR)
    io_loop = IOLoop(_loop=loop)
    server = http_server(io_loop, sock, registration_db=r)
    server.start()

    emitted = []

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

    r.bind_all(cb)

    client = LookupClient(loop, "ws://%s/ws" % LOOKUPD_ADDR)
    client.start()

    messages = []
    messages.append(client.identify("c1", "broadcast", 1.0))
    messages.append(client.ping())
    messages.append(client.add_job("a.job1"))
    messages.append(client.add_process("a.job1", 1))
    messages.append(client.remove_process("a.job1", 1))
    messages.append(client.remove_job("a.job1"))

    t0 = pyuv.Timer(loop)
    t0.start(lambda h: client.close(), 0.4, 0.0)

    def stop(h):
        h.close()
        server.stop()
        io_loop.close()

    t = pyuv.Timer(loop)
    t.start(stop, 0.6, 0.0)
    loop.run()

    assert len(messages) == 6
    results = ["ok" in msg.result() for msg in messages]
    assert results == [True, True, True, True, True, True]

    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 == {}