Coder Social home page Coder Social logo

blockingqueue's Introduction

BlockingQueue

Build Status Inline docs

BlockingQueue is a simple queue implemented as a GenServer. It has a fixed maximum length established when it is created.

The queue is designed to decouple, but limit, the latency between a producer and consumer. When pushing to a full queue the push operation blocks preventing the producer from making progress until the consumer catches up. Likewise, when calling pop on an empty queue the call blocks until there is work to do.

Installation

Add a dependency in your mix.exs:

deps: [{:blocking_queue, "~> 1.0"}]

Examples

A simple example:

{:ok, pid} = BlockingQueue.start_link(5)
BlockingQueue.push(pid, "Hi")
BlockingQueue.pop(pid) # should return "Hi"

The queue is designed to be used from more complex examples in which the producer and consumer are in separate processes and run asynchronously to each other.

An example of an infinite stream:

{:ok, pid} = BlockingQueue.start_link(:infinity)
BlockingQueue.push(pid, "Hi")
BlockingQueue.pop(pid) # should return "Hi"

An example using the Stream API

{:ok, pid} = BlockingQueue.start_link(5)

[1, 2, 3]
|> BlockingQueue.push_stream(pid)

BlockingQueue.pop_stream(pid)
|> Enum.take(3)  # Should return [1, 2, 3]

Contribute

Just fork the repo, make your change, and send me a pull request.

Or, feel free to file and issue and start a discussion about a new feature you have in mind.

blockingqueue's People

Contributors

benfalk avatar brandonhamilton avatar c-rack avatar cboggs avatar joekain avatar wtfleming avatar

Watchers

 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.