Coder Social home page Coder Social logo

dispatcher's Introduction

Go Report Card GoDoc Build status

Dispatcher

Dispatcher provides a concurrent tasks treating functionality based on go-routines.

After you initialize and run dispatcher it is ready to perform tasks.

You can add tasks via AddWork() func. Both Run() and AddWork() must be called in separate go-routines. Tasks will be treated concurrently but amount of workers will be limited to the parameter specified at the dispatcher initialization call New().

To stop the dispatcher you can use the Stop() func. But you should be careful with it: just after this call the dispatcher stops receiving new work and stops returning results even if the task was already processed but result were not yet sent into appropriate channel. When Stop() was called, dispatcher flushes channels and exits.

If you need just to do tons of work with dispatcher and quit after without waiting for next portions of work, you can use the WaitUntilNoTasks(period) func. It checks the number of currently processing tasks each period of seconds. If it seems that dispatcher is not processing any task, it stops.

The Treat() by default is not thread-safe and one dispatcher performs multiple of Treat at a time. Be careful using shared memory in it to avoid data races. The onResultFunc also is not thread-safe. You should control safety by yourself.

You can use the second dispatcher and organize two dispatchers in a conveyor pattern using Treat in first dispatcher to treat first stage data, and in the onResultFunc of the first dispatcher you may call go AddWork(...) to send the result to the second dispatcher. The second dispatcher will treat the data from the first's onResultFunc in it's own Treat of the returned element and return it in it's own onResultFunc, this one of a second stage dispatcher. And so on: you can organize it in more stages.

For example of one-staged using please look into the tests.

dispatcher's People

Contributors

mtfelian avatar

Stargazers

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