Code Monkey home page Code Monkey logo

channelpy's Introduction

ChannelPy

A basic implementation of Go inspired channels.

Use these channels to communicate across Python threads, processes, or hosts, independent of network location. Channels are first class objects that can be passed themselves through the channels.

The current implementation uses RabbitMQ as a broker, but it can be swapped by other queues or even a peer-to-peer transport mechanism such as ZeroMQ. See the implementation details.

Prerequisite

Start a RabbitMQ instance by executing the following command in the root directory of this repository:

docker-compose up -d

Quickstart

  • Create a channel with:

    >>> from channelpy import Channel
    >>> ch = Channel()
  • Put and get objects to and from the channel:

    >>> ch.put('foo')
    >>> ch.get()
    'foo'
    >>> ch.get(timeout=1)
    # raises ChannelTimeoutException since channel is empty
  • Pass channels into channels:

    >>> ch1 = Channel()
    >>> ch1.put(5)
    >>> ch2 = Channel()
    >>> ch2.put(ch1)
    >>> x = ch2.get()
    >>> x.get()
    5
  • Channels can be instantiated by name:

    >>> ch = Channel()
    >>> ch.name
    'daa0a490f9254c69883335c9f925d74f'
    >>> another = Channel(name=ch.name)
    >>> another.put('foo')
    >>> ch.get()
    'foo'

    Or create them with a specific name:

    >>> ch = Channel(name='my_channel')
  • Multiple consumers and producers can be attached to a channel. Messages are delivered in round-robin fashion to consumers. However, a close_all can be delivered to all the consumers to signal a full termination of the channel.

    >>> ch = Channel()
    >>> def f(c):
    ...     while True:
    ...         print(c.get())
    ...
    >>> threading.Thread(target=f, args=(ch,)).start()
    >>> threading.Thread(target=f, args=(ch,)).start()
    >>> ch.put(4)        # one of the threads will print 4
    >>> ch.close_all()   # ChannelClosedException is raised in all threads
  • The broker to use can be configured at instantiation time or by using the config file ~/.channelpy.yml. For example:

    connection: RabbitConnection
      uri: amqp://192.168.35.10:5672

Tests

Run the tests with:

$ BROKER='amqp://localhost:5672' py.test -v

Implementation Details

To be written.

channelpy's People

Contributors

waltermoreira 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.