Menu

Named tuples are lost when sending them to a remote coroutine

2016-10-11
2016-10-12
  • Oscar Curero

    Oscar Curero - 2016-10-11

    Hi!

    I stumbled across a a very strange problem. If I try to send a named tuple from one coroutine to another, the tuple never arrives to the remote coroutine (no error is printed). However, if a simple tuple is used, the message is sent correctly.

    I think that the problem is not present on the channels, because I had some named tuples working fine with channels and I don't remember having any problems.

    Testa.py (server):

    import asyncoro.disasyncoro as asyncoro
    import collections                                                                                                                                             
    
    def hello1(coro=None):
        coro.register()
        while True:
            msg = yield coro.receive()
            print msg
    
    scheduler = asyncoro.AsynCoro(node="127.0.0.1",
                                  udp_port=51350,
                                  tcp_port=10000,
                                  name="A")
    
    asyncoro.Coro(hello1)
    

    Testb.py:

    import asyncoro.disasyncoro as asyncoro
    import collections                     
    
    test = collections.namedtuple('TEST', ['num1','num2'])
    
    def client(coro=None):
        server = yield scheduler.peer(asyncoro.Location("127.0.0.1", 10000))
        rmt = yield coro.locate("hello1", timeout=5)
        print rmt
        rsn = yield rmt.deliver((1,1), 5)
        if rsn == 1:
            print "Works!"
        rsn = yield rmt.deliver(test(1,1), 5)
        if rsn == -1:
            print "Fails!"
    
    scheduler = asyncoro.AsynCoro(node="127.0.0.1",
                                  udp_port=51350,
                                  tcp_port=10001,
                                  name="B")
    asyncoro.Coro(client)
    

    This is the output (both servers are running the latest asyncoro version):

    2016-10-12 00:36:49 asyncoro - version 4.3.1 with epoll I/O notifier
    2016-10-12 00:36:49 asyncoro - network server "B" @ 127.0.0.1:10001, udp_port=51350
    ~hello1/140385367811008@127.0.0.1:10000
    Works!
    Fails!
    

    Thanks! :)

     
  • Giridhar Pemmasani

    Tuple (or any object) can't be sent over network; they have to be serialized and the remote side then have to deserialize (which means the definitions available at both sides with proper name). Following programs should work.

    Testa.py:

    import asyncoro.disasyncoro as asyncoro
    import collections
    TEST = collections.namedtuple('TEST', ['num1','num2'])
    
    def hello1(coro=None):
        coro.register()
        while True:
            msg = yield coro.receive()
            if isinstance(msg, str): # in this case, assume serialized data (in a generic case this needs to be modified)
                msg = asyncoro.deserialize(msg)
                print('%s / %s' % (type(msg), msg))
    
    scheduler = asyncoro.AsynCoro(node="127.0.0.1",
                                  udp_port=51350,
                                  tcp_port=10000,
                                  name="A")
    
    asyncoro.Coro(hello1)
    

    and Testb.py:

    import asyncoro.disasyncoro as asyncoro
    import collections
    
    TEST = collections.namedtuple('TEST', ['num1','num2'])
    
    def client(coro=None):
        server = yield scheduler.peer(asyncoro.Location("127.0.0.1", 10000))
        rmt = yield coro.locate("hello1", timeout=5)
        print rmt
        rsn = yield rmt.deliver((1,1), 5)
        if rsn == 1:
            print "Works!"
        obj = TEST(1,2)
        rsn = yield rmt.deliver(asyncoro.serialize(obj), 5)
        if rsn == -1:
            print "Fails!"
    
    scheduler = asyncoro.AsynCoro(node="127.0.0.1",
                                  udp_port=51350,
                                  tcp_port=10001,
                                  name="B")
    asyncoro.Coro(client)
    
     

    Last edit: Giridhar Pemmasani 2016-10-12

Log in to post a comment.