promise-pc
New functionality! - Tree notify, see below
A Promise implementation of the producer-consumer problem.
Basically a queue in which you can keep adding promises. Consumer(s) consume promises and notify the producer. Uses the then/promise implementation.
Number of consumers is dynamic. (Coming soon. Currently changing the maxParallel after initialization does not do anything)
Usage
- Require the module and initialize a PCQueue
var PCQueue = PCQueue;var pcqueue = opts;
See below for available options
- When an item is ready to be consumed, a producer pushes it onto the queue using the produce method.
var promise = pcqueue; promise;
Note that the consumer function is not immediately invoked. Only when a consumer is free (as indicated by the maxParallel option).
Tree Notify
Many a times processing of a tree or tree like structure requires async processing. On a node per node basis there is no difference in production and consumption. But at a tree level, it may be needed to be notified when tree processing is complete. The treeNotify event can be used to be notified once all the nodes of a tree are finished being processed by the consumer.
This is done by
- Declaring tree option as true
- Using treeNotify event to be notified of tree processing completion
- During producer/consumer processing, using children method to declare number of children current node has.
See tree test in tests.js
pcqueue; //Inside producer/consumer processingpcqueue;
Options
- maxParallel : Maximum number of consumers. If not specified, assumes inifite number of consumers
- tree : Enables tree mode wherein treeNotify event is available