Code Monkey home page Code Monkey logo

pycommunicator's Introduction

pyCommunicator

Library to make asynchronous-statefull-bidirectional communication with external software.

This is the most complex example, you can find easier examples in this project

     
    #main.py
     
    from pyCommunicator.FrontEndJsonCallback import FrontEndJsonCallback

    #function called when output of the first command is ready
    def onRead1(output):
        print "1 " +  str(output)

    #function called when output of the second command is ready
    def onRead2(output):
        print "2 " + str(output)
    
    #function called when output of the third command is ready
    def onRead3(output):
        print "3 " + str(output)
        
    #the cmd command
    cmd  = ['python','ext.py']

    #create a new communication channel between this and an external python file
    # multiple requests from c channel will be enqueued and processed inline
    c = FrontEndJsonCallback(cmd)
    c.run()
    
    #create a new communication channel between this and an external python file
    # multiple requests from d channel will be enqueued and processed inline
    d = FrontEndJsonCallback(cmd)
    d.run()

    #messages are any type of python's primitive data.
    msg1 = ["Hello",'World','1']
    msg2 = ["Hello",'World','2']
    msg3 = ["Hello",'World','3']

    # start sending array messages to the target file through 2 channels 
    c.write(msg1,onRead1)
    c.write(msg2,onRead2)
    d.write(msg3,onRead3)
    
    # show the not-blocking system
    print 'END'


    ''' print:

    'END'
    3 ['Hello', 'World', '3']
    1 ['Hello', 'World', '1']
    2 ['Hello', 'World', '2']

    '''
    #ext.py
    
    '''
    External echo service with different process time based on command type
    '''
    
    from pyCommunicator.BackEndJsonCallback import BackEndJsonCallback
    import time

    s = BackEndJsonCallback()
    def x(data):
        if data[2] == '1':
            time.sleep(3)
        elif data[2] == '2':
            time.sleep(0)
        elif data[2] == '3':
            time.sleep(1)
        s.reply(data)

    s.onRead += x
    s.run()

pycommunicator's People

Contributors

jstar88 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.