Skip to main content

Go Inspired Channels

Project description

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.


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

docker-compose up -d


  • Create a channel with:

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

    >>> ch.put('foo')
    >>> ch.get()
    >>> 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()
  • Channels can be instantiated by name:

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

    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://


Run the tests with:

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

Implementation Details

To be written.

Project details

Release history Release notifications | RSS feed

This version


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Built Distribution

channelpy-0.2-py2.py3-none-any.whl (9.3 kB view hashes)

Uploaded 2 7

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page