Coder Social home page Coder Social logo

raku-async-workers's Introduction

NAME

Async::Workers - Asynchronous threaded workers

SYNOPSIS

use Async::Workers;

my $wm = Async::Workers.new( :max-workers(5) );

for 1..10 -> $n {
    $wm.do-async: {
        sleep 1.rand;
        say "Worker #$n";
    }
}

await $wm;

DESCRIPTION

This module provides an easy way to execute a number of jobs in parallel while allowing to keep the resources consumed by your code under control.

Both OO and procedural interfaces are provided.

Reliability

This module has been tested by running 20k repetitions of it's test suite using 56 parallel processes. This doesn't prove there're no bugs, but what can be told for certain is that within the tested scenarios the robustness is high enough.

Terminology

  • job is what gets executed. Depending on context, a job could be an instance of Async::Workers::Job class, or a user provided code object.

  • worker is an instance of Async::Workers::Worker class. It is controlling a dedicated thread in which the job code is ran.

  • worker manager or just manager is an instance of Async::Workers class which controls the execution flow and manages workers.

How it works.

The goal is achieved by combining a queue of jobs and a number of pre-spawned threads controlled by workers. A job is picked from the queue by a currently unoccupied worker and the code object associated with it is invoked. Since the number of workers is limited it is easier to predict and plan CPU usage.

Yet, it is still possible to over-use memory and cause heavy swapping or even overflows in cases when there is too many jobs are produced and an average one takes longer to complete than it is needed to generate a new one. To provide some control over this situation one can define hi and lo thresholds on the queue size. When the queue contains as many jobs, as defined by the hi threshold, do-async method blocks upon receiving a new job request and unblocks only when the queue shortens down to its lo threshold.

The worker manager doesn't start workers until the first job is sent to the queue. It is also possible to shutdown all workers if they're no longer needed.

Some internal events are reported with messages from Async::Workers::Msg. See the on_msg method below.

ATTRIBUTES

max-workers

Maximum number of workers. Defaults to $*KERNEL.cpu-cores.

max-jobs

Set the maximum number of jobs a worker should process before stopping and letting the manager to spawn a new one. The functionality is not activated if the attribute is left undefined.

lo-threshold, hi-threshold

Low and high thresholds of the queue size.

queued

Current queue size. If the queue has been blocked due to reaching hi-threshold then jobs awaiting for unblock are not counted toward this value.

running

The number of currently occupied workers.

completed

A Promise which is kept when manager completes all jobs after transitioning into shutdown state. When this happens the job queue is closed and all workers are requested to stop. Submission of a new job with do-async at this point will re-vivify the queue and return the manager into working state.

In case of an internal failure the promise will be broken with an exception.

empty

A Promise which is kept each time the queue gets emptied. Note that the initially empty queue is not reflected with this attribute. Only when the queue contained at least one element and then went down to zero length this promise is kept. In other words, it happens when Async::Workers::Msg::Queue::Empty is emitted.

Immediately after being kept the attribute gets replaced with a fresh Promise. So that the following example will finish only if the queue has been emptied twice:

await $wm.empty;
await $wm.empty;

quiet

If set to True then no exceptions thrown by jobs are reported. In this case it is recommended to monitor messages for Async::Workers::Msg::Job::Died.

METHODS

do-async( &code, |params --> Async::Workers::Job )

Takes a &code object and wraps it into a job object. params are passed to &code when it gets executed.

This method blocks if hi-threshold is defined and the queue size has reached the limit.

If no error happens then the method returns an Async::Workers::Job instance. Otherwise it may throw either X::Manager::Down if the manager is in shutdown or completed status; or it may throw X::Manager::NoQueue if somehow the job queue has not been initialized properly.

shutdown

Switches the manager into shutdown state and closes the job queue. Since the queue might still contain some incomplete jobs it is likely to take some time until the completed promise gets kept. Normally it'd be helpful to await for the manager:

my $wm = Async::Workers.new(...);
...
$wm.shutdown;
await $wm;

In this case the execution blocks until the job queue is emptied. Note that at this point completed might still not been fulfilled because workers are being shutting down in the meanwhile.

workers

Returns the number of started workers.

workers( UInt $num )

Sets the maximum number of workers (max-workers attribute). Can be used at any time without shutting down the manager:

$wm = Async::Worker.new: :max-workers(20);
$wm.do-async: &job1 for ^$repetitions;
$wm.workers($wm.workers - 5);
$wm.do-async: &job2 for ^$repetitions;

If user increases the number of workers then as many additional ones are started as necessary.

On the contrary, if the number of workers is reduced then as many of them are requested to stop as needed to meet user's demand. Note that this is done by injecting special jobs. It means that for a really long queue it may take quite a time before the extra workers receive the stop command. This behaviour may change in the future.

set-threshold( UInt :$lo, Num :$hi )

Dynamically sets high and low queue thresholds. The high might be set to Inf to define unlimited queue size. Note that this would translate into undefined value of hi-threshold attribute.

on_msg( &callback )

Submits a Async::Workers::Msg message object to user code passed in &callback. Internally this method does tapping on a message Supply and returns a resulting Tap object.

The following messages can currently be emitted by the manager (names are shortened to not include Async::Workers::Msg:: prefix):

  • Shutdown - when the manager is switched into shutdown state

  • Complete - when manager completed all jobs and shut down all workers

  • Exception - when an internal failure is intercepted; the related exception object is stored in attribute exception

  • Worker - not emitted, a base class for other Worker messages. Defines attribute worker which contains the worker object

  • Worker::Started - when a new worker thread has started

  • Worker::Complete - when a worker finishes

  • Worker::Died - when a worker throws. exception attribute will then contain the exception thrown. This message normally should not be seen as it signals about an internal error.

  • Queue – not emitted, a base class for other Queue messages. Defines attribute size which contains queue size at the moment when message was emitted.

  • Queue::Inc - queue size inceased; i.e. a new job submitted. Note that if the queue has reached the hi threshold then a job passed to do-async doesn't make it into the queue and thus no message is emitted until the queue is unblocked.

  • Queue::Dec – a job has finished and the queue size is reduced

  • Queue::Full - hi threshold is reached

  • Queue::Low - queue size reduced down to lo threshold

  • Queue::Empty – the queue was emtied

  • Job – not emitted, a parent class of job-related messages. Defines job attribute which holds a Async::Workers::Job object.

  • Job::Enter - emitted right before a worker is about to invoke a job

  • Job::Complete – emitted right after a job finishes

  • Job::Died – when a job throws. exception attribute contains the exception object.

messages

This method produces a Supply which emits messages.

HELPER SUBS

stop-worker($rc?, :$soft = False)

Bypasses to the current worker stop method.

If called from within a job code it would cause the worker controlling the job to stop. If this would reduce the number of workers to less than max-workers then the manager will spawn as many new ones as needed:

$wm.do-async: {
    if $something-went-wrong {
        stop-worker
    }
}

Note that the job would be stopped too, unless :soft parameter is used. In this case both the job and its worker will be allowed to complete. The worker will stop after the job is done.

PROCEDURAL

Procedural interface hides a singleton object behind it. The following subs are exported by the module:

async-workers( |params --> Async::Workers:D )

Returns the singleton object. Creates it if necessary. If supplied with parameters they're passed to the constructor. If singleton is already created then the parameters are ignored.

do-async

Bypasses to the corresponding method on the singleton.

do-async {
    say "My task";
}

shutdown-workers

Bypasses to shutdown on the singleton.

AUTHOR

Vadim Belman [email protected]

LICENSE

Artistic License 2.0

See the LICENSE file in this distribution.

raku-async-workers's People

Contributors

vrurg avatar sjqtentacles avatar

Stargazers

Khalid Mohamed Elborai avatar  avatar Juan Julián Merelo Guervós avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

melezhik

raku-async-workers's Issues

Could not install with zef

$ zef install Async::Workers
===> Searching for: Async::Workers
===> Updating cpan mirror: https://raw.githubusercontent.com/ugexe/Perl6-ecosystems/master/cpan1.json
===> Updating p6c mirror: https://raw.githubusercontent.com/ugexe/Perl6-ecosystems/master/p6c1.json
===> Updated p6c mirror: https://raw.githubusercontent.com/ugexe/Perl6-ecosystems/master/p6c1.json
===> Updated cpan mirror: https://raw.githubusercontent.com/ugexe/Perl6-ecosystems/master/cpan1.json
===> Searching for missing dependencies: AttrX::Mooish
===> Testing: AttrX::Mooish:ver<0.6.2>:auth<github:vrurg>
===> Testing [OK] for AttrX::Mooish:ver<0.6.2>:auth<github:vrurg>
===> Testing: Async::Workers:ver<0.0.1>:auth<github:vrurg>
    # Failed test 'queue reduction count'
    # at t/010-workers.t6 line 57
    # expected: '8'
    #      got: '7'
    # Looks like you failed 1 test of 1
# Failed test 'Limited queue'
# at t/010-workers.t6 line 34
# Looks like you failed 1 test of 3
===> Testing [FAIL]: Async::Workers:ver<0.0.1>:auth<github:vrurg>
Aborting due to test failure: Async::Workers:ver<0.0.1>:auth<github:vrurg> (use --force-test to override)
LizyPro:rakudo.moar liz$ zef install Async::Workers
===> Searching for: Async::Workers
===> Searching for missing dependencies: AttrX::Mooish
===> Testing: AttrX::Mooish:ver<0.6.2>:auth<github:vrurg>
===> Testing [OK] for AttrX::Mooish:ver<0.6.2>:auth<github:vrurg>
===> Testing: Async::Workers:ver<0.0.1>:auth<github:vrurg>
===> Testing [OK] for Async::Workers:ver<0.0.1>:auth<github:vrurg>
===> Installing: AttrX::Mooish:ver<0.6.2>:auth<github:vrurg>
===> Installing: Async::Workers:ver<0.0.1>:auth<github:vrurg>
===> Install [FAIL] for Async::Workers:ver<0.0.1>:auth<github:vrurg>: ===SORRY!===
Could not find Async::Msg at line 167 in:
    inst#/Users/liz/Github/rakudo.moar/install/share/perl6/site
    inst#/Users/liz/Github/rakudo.moar/install/share/perl6/vendor
    inst#/Users/liz/Github/rakudo.moar/install/share/perl6
    ap#
    nqp#
    perl5#

===SORRY!===
Could not find Async::Msg at line 167 in:
    inst#/Users/liz/Github/rakudo.moar/install/share/perl6/site
    inst#/Users/liz/Github/rakudo.moar/install/share/perl6/vendor
    inst#/Users/liz/Github/rakudo.moar/install/share/perl6
    ap#
    nqp#
    perl5#

Tests are unreliable

Sometimes they pass ok and sometimes they do this:

===> Searching for: Async::Workers
===> Found: Async::Workers:ver<0.1.1>:auth<github:vrurg> [via Zef::Repository::Ecosystems<cpan>]
===> Fetching [OK]: Async::Workers:ver<0.1.1>:auth<github:vrurg> to /mnt/data/zef-data/tmp/1603530870.384373.6361.436658255747/Async-Workers-v0.1.1.tar.gz
===> Extraction [OK]: Async::Workers to /mnt/data/zef-data/store/Async-Workers-v0.1.1.tar.gz
===> Testing: Async::Workers:ver<0.1.1>:auth<github:vrurg>
[Async::Workers] t/001-meta.t6 ....... ok
[Async::Workers] t/010-workers.t ..... 
[Async::Workers] Dubious, test returned 1 (wstat 256, 0x100)
[Async::Workers] Failed 1/6 subtests 
[Async::Workers] t/020-procedural.t .. ok
[Async::Workers] Test Summary Report
[Async::Workers] -------------------
[Async::Workers] t/010-workers.t   (Wstat: 256 Tests: 6 Failed: 1)
[Async::Workers]   Failed test:  5
[Async::Workers]   Non-zero exit status: 1
[Async::Workers] Files=3, Tests=9, 13 wallclock secs ( 0.02 usr  0.00 sys + 11.74 cusr  0.76 csys = 12.52 CPU)
[Async::Workers] Result: FAIL
===> Testing [FAIL]: Async::Workers:ver<0.1.1>:auth<github:vrurg>
[Async::Workers] Failed to get passing tests, but continuing with --force-test
===> Installing: Async::Workers:ver<0.1.1>:auth<github:vrurg>
===> Install [OK] for Async::Workers:ver<0.1.1>:auth<github:vrurg>

Given it is "Async::" module, some races are lurking around either in test or actual code.

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.