def main():
    args = aux.parse_args("Echo server with threaded-processing model.")
    server_socket = aux.listening_socket(args.port)

    while True:
        client_socket, client_addr = server_socket.accept()

        worker = Worker(client_socket)
        worker.start()
def main():
    args = aux.parse_args('Echo server with epoll-processing model.')
    server_socket = aux.listening_socket(args.port)
    server_fileno = server_socket.fileno()

    fileno_to_data = {}
    fileno_to_socket = {}

    read_epoll = select.epoll()
    read_epoll.register(server_fileno, select.EPOLLIN)

    while True:
        for fileno, eventmask in read_epoll.poll(0.05):
            if fileno == server_fileno:
                client_socket, _ = server_socket.accept()
                client_fileno = client_socket.fileno()

                fileno_to_data[client_fileno] = ''
                fileno_to_socket[client_fileno] = client_socket

                read_epoll.register(client_fileno, select.EPOLLIN)
            else:
                client_socket = fileno_to_socket[fileno]

                data = client_socket.recv(1024)
                if not data:
                    read_epoll.unregister(fileno)
                    del fileno_to_data[fileno]
                    del fileno_to_socket[fileno]
                    client_socket.close()
                else:
                    fileno_to_data[fileno] += data

        check_writability = [f for f, d in fileno_to_data.iteritems() if d]
        if not check_writability:
            continue

        write_epoll = select.epoll()
        for fileno in check_writability:
            write_epoll.register(fileno, select.EPOLLOUT)

        for fileno, eventmask in write_epoll.poll(0.05):
            if eventmask & (select.EPOLLERR | select.EPOLLHUP):
                read_epoll.unregister(fileno)
                fileno_to_socket[fileno].close()
                del fileno_to_data[fileno]
                del fileno_to_socket[fileno]
                continue

            client_socket = fileno_to_socket[fileno]
            data = fileno_to_data[fileno]

            n = client_socket.send(data)
            if n > 0:
                fileno_to_data[fileno] = data[n:]

        write_epoll.close()
def main():
    args = aux.parse_args('Echo server with fork processing model.')
    server_socket = aux.listening_socket(args.port)

    signal.signal(signal.SIGCHLD, signal.SIG_IGN)

    while True:
        client_socket, client_addr = server_socket.accept()

        if os.fork() == 0:
            server_socket.close()
            aux.communicate(client_socket)
            client_socket.close()
            return
        else:
            client_socket.close()
def main():
    args = aux.parse_args('Echo server with select-processing model.')
    server_socket = aux.listening_socket(args.port)

    socket_to_data = {}
    client_sockets = []
    all_sockets = [server_socket]

    while True:
        readable, _, _ = select.select(all_sockets, [], [], 0.05)

        if server_socket in readable:
            client_socket, _ = server_socket.accept()

            socket_to_data[client_socket] = ''
            client_sockets.append(client_socket)
            all_sockets.append(client_socket)

            readable.remove(server_socket)

        for client_socket in readable:
            data = client_socket.recv(1024)
            if not data:
                del socket_to_data[client_socket]
                client_sockets.remove(client_socket)
                all_sockets.remove(client_socket)
                client_socket.close()
            else:
                socket_to_data[client_socket] += data

        check_writability = [cs for cs in client_sockets
                             if socket_to_data[cs]]

        _, writable, _ = select.select([], check_writability, [], 0.05)

        for client_socket in writable:
            data = socket_to_data[client_socket]
            n = client_socket.send(data)
            if n > 0:
                socket_to_data[client_socket] = data[n:]
Пример #5
0
    # Two sided ledger book
    book = Market()

    # Tracks the value of the last issued sell (income) or buy (expense)
    last_price = dict(sell=0, buy=0)

    # Tracks whether a price has been issued or has been invalidates
    standing_price = dict(sell=False, buy=False)

    # Maps a sell or buy action to the correct side of the leger
    side = dict(sell='B', buy='S')

    # Exit if runtime argument error or help requested
    try:
        target = aux.parse_args(sys.argv)
    except ValueError as e:
        print(e.args[0])
        sys.exit()

    for line in sys.stdin:

        # Read message from logfile, skip if there are formatting errors
        try:
            order = aux.parse_log(line)
        except ValueError as e:
            sys.stderr.write('{0:s}: {1:s}\n'.format(e.args[0], line))
            continue

        # Modify the book based on incomming message
        if order['order_type'] == 'A':