Esempio n. 1
0
async def consumer():
    ch = Channel(chan)
    c = await ch.accept(authkey=syncword.encode())
    myset = set()
    while True:
        try:
            msg = await c.recv()
        except (EOFError, ConnectionResetError
                ) as e:  # in the event that the client closes
            print('resetting')
            myset = set()
            c = await ch.accept(authkey=syncword.encode())
            continue
        if msg is None:  # explicit reset
            myset = set()
        else:
            op, uri = msg.split(' ', 1)
            print(op, uri)
            if op == 'add':
                if uri in myset:
                    await c.send(True)
                else:
                    myset.add(uri)
                    await c.send(False)
            elif op == 'del':
                myset.discard(uri)
                await c.send(False)
            else:
                await c.send('ERROR')
        print(myset)
Esempio n. 2
0
async def timeout_restart(address, timeout_second, task, *args):

    ch = Channel(address)

    needs_start_up = True
    do_loop = True

    while do_loop:
        if needs_start_up:
            # q = UniversalQueue()
            needs_start_up = False

            upload_task = await curio.spawn(start_task, address, task, *args)

            c = await ch.accept(authkey=b'peekaboo')
            print("start listening")
            # ch.connect(authkey=b'peekaboo')

        r = await wait_for_timeout_with_channel(c, timeout_second)
        print(r)

        if r == -1:
            do_loop = False

        elif r == 0:
            # restart
            await upload_task.cancel()
            needs_start_up = True
Esempio n. 3
0
File: rrid.py Progetto: mr-c/scibot
    async def producer():
        chan = ('localhost', 12345)
        ch = Channel(chan)
        c = await ch.connect(authkey=b'hello')

        async def send(uri):
            await c.send(uri)
            resp = await c.recv()
            #await c.close()
            print(resp, uri)
            return resp

        return send
Esempio n. 4
0
File: rrid.py Progetto: mr-c/scibot
async def producer():
    chan = ('localhost', 12345)
    ch = Channel(chan)
    try:
        c = await ch.connect(authkey=syncword.encode())
    except AttributeError:
        raise IOError(
            'Could not connect to the sync process, have you started sync.py?')

    async def send(uri):
        await c.send(uri)
        resp = await c.recv()
        #await c.close()
        print(resp, uri)
        return resp

    return send
Esempio n. 5
0
 def __init__(
     self,
     address,
     port,
     request_handler,
     memoized=True,
     buffer_size=1 << 13,
     parallel=True,
     cpus=None,
     search_path=None,
     worker_subprocess_timeout=600,
 ):
     '''
         address:        the address the listening socket will bind to.
         port:           the port the listening socket will bind to.
         request_handler: an importable async function handling the received request as a dict.
                         If 'parallel' is True, this can be either the dot-path to the function or the function itself.
                         You may need to provide an additional search path in 'search_path' if the handler can't be found. 
                         This usually happens if your handler is defined in a script but not in a library.
         memoized:       True is responses are to be cached. Defaults to True.
         buffer_size:    the maximum amount of data to be received at once from the client connection. Defaults to 8KB.
         parallel:       if True, handle all received requests in parallel, using a pool of processes. Defaults to True.
         cpus:           number of processes if 'parallel' is True. Defaults to None which means match the number of CPUs on the host machine.
         search_path:    path of the module containing the 'request_handler' definition. Will be prepended to sys.path when workers boot.
         worker_subprocess_timeout: timeout in seconds after which the subprocesses will be killed if no new request is queued up.
                         Defaults to 5 seconds.
     '''
     self.address = address
     self.port = port
     self.buffer_size = buffer_size
     self.memoized = memoized
     self.request_handler = request_handler
     self._saved_responses = {}
     self.parallel = parallel
     self.cpus = cpus or cpu_count()
     if parallel:
         self.requests = Queue()
         self.authkey = token_bytes()
         self.search_path = search_path
         # tag the socket path with the pid so we can run the multiple servers,
         # as is the case when running the tests while in a container that runs the official server.
         self.unix_socket_path = f'/var/run/asynctcp.server.channel.{current_process().pid}'
         self.channel = Channel(self.unix_socket_path,
                                family=socket.AF_UNIX)
         self.subprocess_launch_request = Queue(maxsize=self.cpus)
         self.worker_subprocess_timeout = worker_subprocess_timeout
Esempio n. 6
0
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# #############################################################################
# File Name   : producer.py
# Author      : DaiDai
# Mail        : [email protected]
# Created Time: 四  4/ 1 16:51:10 2021
# #############################################################################

from curio import Channel, run


async def producer(ch):
    c = await ch.accept(authkey=b'peekaboo')
    for i in range(10):
        await c.send(i)  # Send some data
    await c.send(None)


if __name__ == '__main__':
    ch = Channel(('localhost', 30000))
    run(producer, ch)
    # >>> python3 producer.py