Beispiel #1
0
def client_proc(computation, task=None):
    # schedule computation with the scheduler; scheduler accepts one computation
    # at a time, so if scheduler is shared, the computation is queued until it
    # is done with already scheduled computations
    if (yield computation.schedule()):
        raise Exception('Could not schedule computation')

    # in dispycos_client6.py, data is sent to each remote task; here, data
    # is broadcast over channel and remote tasks subscribe to it
    data_channel = pycos.Channel('data_channel')
    # not necessary to register channel in this case, as it is sent to remote
    # tasks; if they were to 'locate' it, it should be registered
    # data_channel.register()

    trend_task = pycos.Task(trend_proc)

    rtask_avg = yield computation.run(rtask_avg_proc, data_channel, 0.4,
                                      trend_task, 10)
    assert isinstance(rtask_avg, pycos.Task)
    rtask_save = yield computation.run(rtask_save_proc, data_channel)
    assert isinstance(rtask_save, pycos.Task)

    # make sure both remote tasks have subscribed to channel ('deliver'
    # should return 2 if they both are)
    assert (yield data_channel.deliver('start', n=2)) == 2

    # if data is sent frequently (say, many times a second), enable
    # streaming data to remote peer; this is more efficient as
    # connections are kept open (so the cost of opening and closing
    # connections is avoided), but keeping too many connections open
    # consumes system resources
    yield pycos.Pycos.instance().peer(rtask_avg.location, stream_send=True)
    yield pycos.Pycos.instance().peer(rtask_save.location, stream_send=True)

    # send 1000 items of random data to remote tasks
    for i in range(1000):
        n = random.uniform(-1, 1)
        item = (i, n)
        # data can be sent to remote tasks either with 'send' or
        # 'deliver'; 'send' is more efficient but no guarantee data
        # has been sent successfully whereas 'deliver' indicates
        # errors right away
        data_channel.send(item)
        yield task.sleep(0.02)
    item = (i, None)
    data_channel.send(item)

    yield computation.close()
    data_channel.close()
Beispiel #2
0
def client_proc(task=None):
    # create channel
    channel = pycos.Channel('sum_prod')
    # create tasks to compute sum and product of numbers sent
    sum_task = pycos.Task(seqsum)
    prod_task = pycos.Task(seqprod)
    # subscribe tasks to channel so they receive messages
    yield channel.subscribe(sum_task)
    yield channel.subscribe(prod_task)
    # send 4 numbers to channel
    for _ in range(4):
        r = random.uniform(0.5, 3)
        channel.send(r)
        print('sent %f' % r)
    # send None to indicate end of data
    channel.send(None)
    yield channel.unsubscribe(sum_task)
    yield channel.unsubscribe(prod_task)
Beispiel #3
0
def server_proc(task=None):
    # to illustrate 'transform' function of channel, messages are modified
    def txfm_msgs(name, msg_cid):
        msg, client_id = msg_cid
        # assert name == 'chat_channel'
        # e.g., drop shoutings
        if msg.isupper():
            return None
        if msg == 'joined':
            msg += ' :-)'
        elif msg == 'bye':
            msg = 'left :-('
        else:
            msg = 'says: %s' % msg
        return (msg, client_id)

    channel = pycos.Channel('chat_channel', transform=txfm_msgs)
    channel.register()
    task.set_daemon()
    task.register('chat_server')
    client_id = 1
    while True:
        # each message is a 2-tuple
        cmd, who = yield task.receive()
        # join/quit messages can be sent by clients themselves, but
        # for illustration server sends them instead
        if cmd == 'join':
            channel.send(('joined', client_id))
            who.send(client_id)
            client_id += 1
        elif cmd == 'quit':
            channel.send(('bye', who))
        elif cmd == 'terminate':
            break
    channel.unregister()
    task.unregister()
Beispiel #4
0
    task.set_daemon()
    # until subscribed, client's deliver will block
    yield task.sleep(5)
    # subscribe to channel to get messages
    yield channel.subscribe(task)
    print(' receiver subscribed')
    while True:
        msg = yield task.receive()
        if msg:
            print('Received "%s" from %s at %s' %
                  (msg['msg'], msg['sender'].name, msg['sender'].location))


if __name__ == '__main__':
    # pycos.logger.setLevel(pycos.logger.DEBUG)
    channel = pycos.Channel('2clients')
    # register channel so client can get a reference to it
    channel.register()

    recv = pycos.Task(receiver_proc)

    if sys.version_info.major > 2:
        read_input = input
    else:
        read_input = raw_input
    while True:
        try:
            cmd = read_input('Enter "quit" or "exit" to terminate: ').strip().lower()
            if cmd in ('quit', 'exit'):
                break
        except: