Projet

Général

Profil

microservice2.py

microservice 2 pour le scénario "Mise à jour de variable" - Benjamin Bohard, 31/08/2017 12:04

Télécharger (1,08 ko)

 
1

    
2
import asyncio
3
from os import environ
4
from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
5

    
6

    
7
class Component(ApplicationSession):
8
    """
9
    An application component that publishes an event every second.
10
    """
11

    
12
    async def onJoin(self, details):
13

    
14
        def update_variable(variable, value=0):
15
            result = 'update {} with {}'.format(variable, value)
16
            print(result)
17
            self.publish(u'com.myapp.cipot1', "le microservice1 a fait modifier {} avec {}".format(variable, value))
18
            return "Variable updated"
19

    
20
        #counter = 0
21
        #dico = {}
22

    
23
        await self.register(update_variable, u'com.myapp.update_variable')
24
        #while True:
25
        #    print("publish: com.myapp.cipot1", dico)
26
        #    self.publish(u'com.myapp.cipot1', dico)
27
        #    dico.update({counter: counter})
28
        #    counter += 1
29
        #    await asyncio.sleep(1)
30

    
31

    
32
if __name__ == '__main__':
33
    runner = ApplicationRunner(
34
        environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws"),
35
        u"realm1",
36
    )
37
    runner.run(Component)