GithubHelp home page GithubHelp logo

bssstudio / work-queue Goto Github PK

View Code? Open in Web Editor NEW

This project forked from edofic/work-queue

0.0 2.0 0.0 99 KB

work queue implementations for operations that must not be done in parallel

Scala 100.00%

work-queue's Introduction

WorkQueue

Actor and thread based implementations of safe work queue. You need something like this when you have multiple threads(or actors) going on and you need to schedule multiple operations/jobs that must not happen in parallel. But you don't want to mess with locks. The go-to solution is to make an actor and a message that instructs it to do something and as (akka) actors are single-threaded you have a guarantee that your jobs will be processe sequentially. But this leads to a bloat of one-off messages and actors. So here are generic solutions.

Usage

import com.edofic.workqueue._
val safely: WorkQueue = ... //see below for instantiation

safely unit {
    //do some dangerous operation long running operation using 'value'
}

//and in other thread
val result: Future[Value] = safely future {
    val value = ... //read something from 'value'
    value
}

You know that these two blocks will never run at the same time - safely acts as a lock. Or if you prefer working with ExecutionContext directly

implicit val ec = safely.executionContext
val f1 = Future( ... )
val f2 = Future( ...)

f1 and f2 will be executed sequentially.

Don't forget to clean up

safely.close()

Actor based

Use an existing ActorSystem and share it's thread pool. Queue is and actor's mailbox and some plumbing that makes for a more fluid API. Very lightweight. Don't do blocking or long running operations with this since you will starve other actors in the system.

val safely = new ActorWorkQueue(system)
//or if creating from actor
val safely = new ActorWorkQueue(context)

Thread based

Create a thread(more precisely: a java single thread executor) to run on. Better suited for blocking or long running operations. But heavier on resources. You don't want thousands of theese running.

val safely = new SingleThreadExecutionContext()

work-queue's People

Contributors

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