Exemple #1
0
# Get status (but wait first)
time.sleep(0.3)
print(sub2.recv())

# Ask status again (simply gives last message)
print(sub2.recv())


## Using received signals


def on_new_state(channel):
    state = channel.recv()
    print("%i received state %s" % (id(channel), state))
    if state == "stop":
        yoton.stop_event_loop()


# Bind
sub1.received.bind(on_new_state)
sub2.received.bind(on_new_state)

# Have some calls made
yoton.call_later(pub1.send, 1.0, "hello")
yoton.call_later(pub1.send, 1.5, "there")
yoton.call_later(pub1.send, 2.0, "now")
yoton.call_later(pub1.send, 2.5, "stop")

# Go!
yoton.start_event_loop()
Exemple #2
0
    def test_req_rep2(self):

        # Test multiple requesters and multiple repliers

        # Connect
        if True:
            self._context1.bind("localhost:test1")
            self._context3.connect("localhost:test1")
            #
            self._context1.bind("localhost:test2")
            self._context2.connect("localhost:test2")
        else:
            freeNode = yoton.Context()
            #
            self._context1.bind("localhost:test1")
            freeNode.connect("localhost:test1")
            freeNode.bind("localhost:test1f")
            self._context3.connect("localhost:test1f")
            #
            self._context1.bind("localhost:test2")
            self._context2.connect("localhost:test2")
        time.sleep(0.1)

        # Turn on 2 requesters and 3 repliers
        self._channel_rep1.set_mode("thread")  # threaded because simulating
        self._channel_rep2.set_mode("thread")  # different processes
        self._channel_rep3.set_mode("thread")

        # Define and register reply handler
        def reply_handler(future):
            reply = future.result()
            reqnr = future.reqnr

            echo, id = reply
            if echo.lower() == "stop":
                yoton.stop_event_loop()
            else:
                self.assertEqual(echo[:3], "msg")
                contextnr = {
                    self._context1.id: 1,
                    self._context2.id: 2,
                    self._context3.id: 3,
                }[long(id, 16)]
                print("request %s from %i handled by context %i." %
                      (echo, reqnr, contextnr))

        # Get echo functions
        echoFun1 = self._channel_req1.echo
        echoFun2 = self._channel_req2.echo

        # Send requests on req 1
        sleepTimes = [1, 0.1, 1, 0.6, 0.6, 0.6, 0.6, 0.6]
        for i in range(len(sleepTimes)):
            f = echoFun1("msg%i" % i, sleepTimes[i])
            f.add_done_callback(reply_handler)
            f.reqnr = 1

        # Send requests on req 2
        sleepTimes = [0.4, 0.4, 0.4, 0.4, 0.4]
        for i in range(len(sleepTimes)):
            f = echoFun2("msg%i" % i, sleepTimes[i])
            f.add_done_callback(reply_handler)
            f.reqnr = 2

        # Stop
        f = echoFun1("stop")
        f.add_done_callback(reply_handler)
        f.reqnr = 1

        # Enter event loop
        yoton.start_event_loop()
Exemple #3
0
 def test_req_rep2(self):
     
     # Test multiple requesters and multiple repliers
     
     # Connect
     if True:
         self._context1.bind('localhost:test1')
         self._context3.connect('localhost:test1')
         #
         self._context1.bind('localhost:test2')
         self._context2.connect('localhost:test2')
     else:
         freeNode = yoton.Context()
         #
         self._context1.bind('localhost:test1')
         freeNode.connect('localhost:test1')
         freeNode.bind('localhost:test1f')            
         self._context3.connect('localhost:test1f')
         #
         self._context1.bind('localhost:test2')
         self._context2.connect('localhost:test2')
     time.sleep(0.1)
     
     # Turn on 2 requesters and 3 repliers
     self._channel_rep1.set_mode('thread') # threaded because simulating
     self._channel_rep2.set_mode('thread') # different processes
     self._channel_rep3.set_mode('thread')
     
     # Define and register reply handler
     def reply_handler(future):
         reply = future.result()
         reqnr = future.reqnr
         
         echo, id = reply
         if echo.lower() == 'stop':
             yoton.stop_event_loop()
         else:
             self.assertEqual(echo[:3], 'msg')
             contextnr = {   self._context1.id:1, 
                             self._context2.id:2, 
                             self._context3.id:3}[long(id,16)]
             print('request %s from %i handled by context %i.' % 
                                             (echo, reqnr, contextnr))
     
     # Get echo functions
     echoFun1 = self._channel_req1.echo
     echoFun2 = self._channel_req2.echo
     
     # Send requests on req 1
     sleepTimes = [1, 0.1, 1, 0.6, 0.6, 0.6, 0.6, 0.6]
     for i in range(len(sleepTimes)):
         f = echoFun1('msg%i'%i, sleepTimes[i]) 
         f.add_done_callback(reply_handler)
         f.reqnr = 1
     
     # Send requests on req 2
     sleepTimes = [0.4, 0.4, 0.4, 0.4, 0.4]
     for i in range(len(sleepTimes)):
         f = echoFun2('msg%i'%i, sleepTimes[i]) 
         f.add_done_callback(reply_handler)
         f.reqnr = 2
     
     # Stop
     f = echoFun1('stop')
     f.add_done_callback(reply_handler)
     f.reqnr = 1
     
     # Enter event loop
     yoton.start_event_loop()