Пример #1
0
def custom_feeder(input, coro=None):
    def write_proc(fin, pipe, coro=None):
        while True:
            data = yield os.read(fin.fileno(), 8 * 1024)
            if not data:
                break
            n = yield pipe.write(data, full=True)
            assert n == len(data)
        fin.close()
        pipe.stdin.close()

    def read_proc(pipe, coro=None):
        # output from sha1sum is small, so read until EOF
        data = yield pipe.stdout.read()
        pipe.stdout.close()
        raise StopIteration(data)

    if platform.system() == 'Windows':
        # asyncfile.Popen must be used instead of subprocess.Popen
        pipe = asyncfile.Popen([r'\cygwin64\bin\sha1sum.exe'],
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE)
    else:
        pipe = subprocess.Popen(['sha1sum'],
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE)

    async_pipe = asyncfile.AsyncPipe(pipe)
    reader = asyncoro.Coro(read_proc, async_pipe)
    writer = asyncoro.Coro(write_proc, open(input), async_pipe)
    stdout = yield reader.finish()
    print('     feeder sha1sum: %s' % stdout)
Пример #2
0
def client_proc(coro=None):
    channel = asyncoro.Channel('sum_prod')
    sum_coro = asyncoro.Coro(seqsum)
    prod_coro = asyncoro.Coro(seqprod)
    yield channel.subscribe(sum_coro)
    yield channel.subscribe(prod_coro)
    for x in range(4):
        r = random.uniform(0.5, 3)
        channel.send(r)
        print('sent %f' % r)
    channel.send(None)
    yield channel.unsubscribe(sum_coro)
    yield channel.unsubscribe(prod_coro)
Пример #3
0
def server(host, port, coro=None):
    coro.set_daemon()
    sock = asyncoro.AsyncSocket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((host, port))
    sock.listen(5000)

    while True:
        conn, addr = yield sock.accept()
        asyncoro.Coro(process, conn)
Пример #4
0
#!/usr/bin/env python

# program for creating coroutines (asynchronous concurrent
# programming); see http://asyncoro.sourceforge.net/tutorial.html for
# details.

import sys, random, time
if sys.version_info.major >= 3:
    import asyncoro3 as asyncoro
else:
    import asyncoro


def coro_proc(n, coro=None):
    s = random.uniform(0.5, 3)
    print('%f: coroutine %d sleeping for %f seconds' % (time.time(), n, s))
    yield coro.sleep(s)
    print('%f: coroutine %d terminating' % (time.time(), n))


for i in range(10):
    asyncoro.Coro(coro_proc, i)
Пример #5
0
        # output from sha1sum is small, so read until EOF
        data = yield pipe.stdout.read()
        pipe.stdout.close()
        raise StopIteration(data)

    if platform.system() == 'Windows':
        # asyncfile.Popen must be used instead of subprocess.Popen
        pipe = asyncfile.Popen([r'\cygwin64\bin\sha1sum.exe'],
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE)
    else:
        pipe = subprocess.Popen(['sha1sum'],
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE)

    async_pipe = asyncfile.AsyncPipe(pipe)
    reader = asyncoro.Coro(read_proc, async_pipe)
    writer = asyncoro.Coro(write_proc, open(input), async_pipe)
    stdout = yield reader.finish()
    print('     feeder sha1sum: %s' % stdout)


# asyncoro.logger.setLevel(logging.DEBUG)

# simpler version using 'communicate'
coro = asyncoro.Coro(communicate, sys.argv[1])
coro.value()  # wait for it to finish

# alternate version with custom read and write processes
asyncoro.Coro(custom_feeder, sys.argv[1])
Пример #6
0
    import asyncoro

def process(conn, coro=None):
    msg = "HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nPong!\r\n"
    if sys.version_info.major >= 3:
        msg = bytes(msg, 'ascii')
    yield conn.sendall(msg)
    conn.close()

def server(host, port, coro=None):
    coro.set_daemon()
    sock = asyncoro.AsyncSocket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((host, port))
    sock.listen(5000)

    while True:
        conn, addr = yield sock.accept()
        asyncoro.Coro(process, conn)

if __name__ == '__main__':
    asyncoro.Coro(server, '127.0.0.1', 8010)

    while True:
        try:
            cmd = sys.stdin.readline().strip().lower()
            if not cmd or cmd == 'exit' or cmd == 'quit':
                break
        except KeyboardInterrupt:
            break
Пример #7
0
#!/usr/bin/env python

# client and server coroutines communicating with message passing
# (asynchronous concurrent programming);
# see http://asyncoro.sourceforge.net/tutorial.html for details.

import sys, random
if sys.version_info.major >= 3:
    import asyncoro3 as asyncoro
else:
    import asyncoro

def server_proc(coro=None):
    coro.set_daemon()
    while True:
        msg = yield coro.receive()
        print('processing %s' % (msg))

msg_id = 0

def client_proc(server, n, coro=None):
    global msg_id
    for x in range(3):
        yield coro.suspend(random.uniform(0.5, 3))
        msg_id += 1
        server.send('%d: %d / %d' % (msg_id, n, x))

server = asyncoro.Coro(server_proc)
for i in range(10):
    asyncoro.Coro(client_proc, server, i)
Пример #8
0
    # each client sends 3 messages
    for j in range(2):
        server.send((i, j))
    server.send((i, None))


def server_proc(n, coro=None):
    k = 0
    while True:
        i, j = yield coro.receive()
        if j is None:
            # client 'i' is done
            k += 1
            if k == n:
                break


if __name__ == '__main__':
    n = int(sys.argv[1]) if len(sys.argv) > 1 else 10000

    start = time.time()
    server = asyncoro.Coro(server_proc, n)
    # create given number of client coroutines
    for i in range(n):
        asyncoro.Coro(client_proc, i, server)
    print('creating %d coroutines took %.3f sec' % (n, time.time() - start))
    proc_start = time.time()
    # wait for server to finish
    server.value()
    print('messaging took %.3f sec' % (time.time() - proc_start))
Пример #9
0
    print('sum: %f' % result)


def seqprod(coro=None):
    # compute product of numbers received over channel
    result = 1
    while True:
        msg = yield coro.receive()
        if msg is None:
            break
        result *= msg
    print('prod: %f' % result)


def client_proc(coro=None):
    channel = asyncoro.Channel('sum_prod')
    sum_coro = asyncoro.Coro(seqsum)
    prod_coro = asyncoro.Coro(seqprod)
    yield channel.subscribe(sum_coro)
    yield channel.subscribe(prod_coro)
    for x in range(4):
        r = random.uniform(0.5, 3)
        channel.send(r)
        print('sent %f' % r)
    channel.send(None)
    yield channel.unsubscribe(sum_coro)
    yield channel.unsubscribe(prod_coro)


asyncoro.Coro(client_proc)
Пример #10
0
    import asyncoro


def server_proc(n, sock, coro=None):
    for i in range(n):
        msg, addr = yield sock.recvfrom(1024)
        print('Received "%s" from %s:%s' % (msg, addr[0], addr[1]))
    sock.close()


def client_proc(host, port, coro=None):
    sock = asyncoro.AsyncSocket(
        socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
    msg = 'client socket: %s' % (sock.fileno())
    if sys.version_info.major >= 3:
        msg = bytes(msg, 'ascii')
    yield sock.sendto(msg, (host, port))
    sock.close()


if __name__ == '__main__':
    sock = asyncoro.AsyncSocket(
        socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
    sock.bind(('127.0.0.1', 0))
    host, port = sock.getsockname()

    n = 50
    server_coro = asyncoro.Coro(server_proc, n, sock)
    for i in range(n):
        asyncoro.Coro(client_proc, host, port)
Пример #11
0
#!/usr/bin/env python

# client program for sending requests to server (tut_sock_server.py)
# with sockets (asynchronous network programming);
# see http://asyncoro.sourceforge.net/tutorial.html for details.

import sys, socket, random
if sys.version_info.major >= 3:
    import asyncoro3 as asyncoro
else:
    import asyncoro

def client(host, port, n, coro=None):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock = asyncoro.AsyncSocket(sock)
    yield sock.connect((host, port))
    msg = '%d: ' % n + '-' * random.randint(100,300) + '/'
    if sys.version_info.major >= 3:
        msg = bytes(msg, 'ascii')
    yield sock.sendall(msg)
    sock.close()

# run 10 client coroutines
for n in range(10):
    asyncoro.Coro(client, '127.0.0.1', 8010, n)
Пример #12
0
            break
        if not line:
            break
        print('%s' % line),
        nlines += 1
    print('lines: %s' % nlines)
    raise StopIteration(nlines)

# asyncoro.logger.setLevel(logging.DEBUG)
if platform.system() == 'Windows':
    # asyncfile.Popen must be used instead of subprocess.Popen
    p1 = asyncfile.Popen([r'\cygwin64\bin\grep.exe', '-i', 'error'],
                         stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    p2 = asyncfile.Popen([r'\cygwin64\bin\wc.exe'], stdin=p1.stdout, stdout=subprocess.PIPE)
    async_pipe = asyncfile.AsyncPipe(p1, p2)
    asyncoro.Coro(writer, async_pipe, r'\tmp\grep.inp')
    asyncoro.Coro(line_reader, async_pipe)
    # in Windows child process may not terminate when input is closed -
    # so terminate it explicitly when pipe is done with:
    # p1.terminate()
    # p2.terminate()
else:
    p1 = subprocess.Popen(['grep', '-i', 'error'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    p2 = subprocess.Popen(['wc'], stdin=p1.stdout, stdout=subprocess.PIPE)
    async_pipe = asyncfile.AsyncPipe(p1, p2)
    asyncoro.Coro(writer, async_pipe, '/var/log/syslog')
    asyncoro.Coro(line_reader, async_pipe)

    # alternate example:

    # p1 = subprocess.Popen(['tail', '-f', '/var/log/kern.log'], stdin=None, stdout=subprocess.PIPE)
Пример #13
0
def server_proc(conn, coro=None):
    # conn is a synchronous socket (as it is obtained from synchronous
    # 'accept'); it's file-descriptor is converted to asynchronous
    # file to read data from that
    afd = asyncfile.AsyncFile(conn)
    csum = hashlib.sha1()
    nlines = 0
    while True:
        # read lines from data
        line = yield afd.readline()
        if not line:
            break
        csum.update(line)
        nlines += 1
    afd.close()
    print('server csum: %s' % (csum.hexdigest()))
    print('lines: %s' % (nlines))


asyncoro.logger.setLevel(logging.DEBUG)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 0))
sock.listen(5)
host, port = sock.getsockname()
print('host: %s, port: %s' % (host, port))

asyncoro.Coro(client_proc, host, port, sys.argv[1])

conn, addr = sock.accept()
asyncoro.Coro(server_proc, conn)