Sources on this Page

> Headlines by Category

 Home / Technology / Software


You are using the plain HTML view, switch to advanced view for a more complete experience.

3D Spukendes Halloween Bildschirmschoner
Advanced Set of toolbar and menu icons
Problems using module Async

    Hi,what I want to do is write some daemon with a defined count of worker processes. To keep things readable I will only post a minimal example of the problem I'm facing (some kind of worker queue):

    !/usr/bin/perluse strict;use warnings;use Async;my $workercount = 10;my @workerqueue;for my $i (1..$workercount) {my $proc = Async->new( sub { sleep($i*3); print "$in"; } );print "--> enqueue $proc->{PID}n";push @workerqueue, $proc;}while (1) {sleep(1);my @unfinished;while (my $proc = shift @workerqueue) {if ($proc and $proc->ready) {print "--> $proc->{PID} is readyn";if (my $e = $proc->error) {print "Something went wrong. The error was: $en";}undef $proc;} else {push @unfinished, $proc;}}push @workerqueue, @unfinished;## START refill queue block ##my $free = $workercount - @workerqueue;for my $i (1..$free) {my $proc = Async->new( sub { sleep($i*3); print "$i newn"; } +);print "--> enqueue $proc->{PID}n";push @workerqueue, $proc;}## END refill queue block ##print "mainloopn";}

    Actually if I comment out the "refill queue block" everything works as expected, the output looks something like:

    --> enqueue 28304--> enqueue 28305--> enqueue 28306--> enqueue 28307--> enqueue 28308--> enqueue 28309--> enqueue 28310--> enqueue 28311--> enqueue 28312--> enqueue 28313mainloopmainloop1--> 28304 is readymainloopmainloopmainloop2--> 28305 is readymainloopmainloopmainloop3--> 28306 is readymainloopmainloopmainloop4--> 28307 is readymainloopmainloopmainloop5--> 28308 is readymainloopmainloopmainloop6--> 28309 is readymainloopmainloopmainloop7--> 28310 is ready...

    But as soon as I include the refill queue block it looks like:

    --> enqueue 28319--> enqueue 28320--> enqueue 28321--> enqueue 28322--> enqueue 28323--> enqueue 28324--> enqueue 28325--> enqueue 28326--> enqueue 28327--> enqueue 28328mainloopmainloop1--> 28319 is ready--> enqueue 28329mainloopmainloopmainloop21 new--> 28320 is ready--> 28327 is ready--> 28328 is ready--> 28329 is ready--> enqueue 28330--> enqueue 28331--> enqueue 28332--> enqueue 28333mainloop--> 28321 is ready--> 28322 is ready--> 28323 is ready--> 28324 is ready--> 28325 is ready--> 28326 is ready--> enqueue 28334--> enqueue 28335--> enqueue 28336--> enqueue 28337--> enqueue 28338--> enqueue 28339mainloopmainloop1 new--> 28330 is ready--> enqueue 28340...

    So what is happening here?As far as my debugging got me, I can tell that for any reason all running processes are considered to be ->ready() as soon as I start a new process out of the main loop.This leads to cleanup and therefore process being killed, so that it never gets a chance to print its output, after that a new process is created in the workerqueue.Since all previous processes are considered ready at a time this leads to the "--> enqueue ..." spam.

    Now the really interesting question:What am I doing wrong so that Async module considers my processes been ready?

    Note: I tried reading through the Async module and as for lets say 90% I get what it is doing. I tried commenting out the following line, but it didn't change anything:

    kill 9 => $pid; # I don't care.

    EDIT: Refering to http://www.perlmonks.org/?node_id=966939 , the example posted by Tanktalus sounds absolutely like what I need. However looking at http://perldoc.perl.org/perlthrtut.html and considering the hint of arpad.szasz: "It seems You are mixing old-style and deprecated threads model(Thread module) with the new ithreads thread model..." I'm wondering. Isn't Tanktalus' example therefore also using the depracated Version of Threads? Is there an equivalent implementaion with the newer ithreads? And could this code be modified to share (or better return to) data with its parent process?

    EDIT 2: I think I will now use something like this (unless anyone has a good reason not to do so):

    #!/usr/bin/perluse strict;use warnings;use threads;use threads::shared;use Data::Dumper;my %state :shared;my $workercount = 10;my $run = 1;$SIG{TERM} = sub { $run = 0; };$SIG{INT} = sub { $run = 0; };my @workerqueue;for my $i (1..$workercount) {my $thr = threads->create(&mysub, $i);my $id = $thr->tid();print "--> enqueue $idn";$state{$id} = 'running';push @workerqueue, $thr;}while ($run) {sleep(1);my @unfinished;while (my $thr = shift @workerqueue) {my $id = $thr->tid();if ($thr and $state{$id} eq 'finished') {print "--> $id is readyn";my $x = $thr->join();delete $state{$id};print Dumper $x; # do something useful with the data} else {push @unfinished, $thr;}}push @workerqueue, @unfinished;## START refill queue block ##my $free = $workercount - @workerqueue;for my $i (1..$free) {my $thr = threads->create(&mysub, $i);my $id = $thr->tid();print "--> enqueue $idn";$state{$id} = 'running';push @workerqueue, $thr;}## END refill queue block ##print "mainloopn";}my @threads = threads->list();foreach my $thr (@threads) {$thr->join();}sub mysub {my $i = shift;$i *= 3;sleep $i;my $id = threads->tid();print "$id : slept for $i secn";my $x = {'ID' => $id,'a' => [1,2,3],'B' => { 'a' => 'A', },};$state{$id} = 'finished';return $x;}
32. XYplorer
Spiceworks IT Desktop is the only application that combines Network Inventory, Help Desk and more in a single, easy-to-use interface.
Emulating Python's unittests MagicMock

    I've been writing a lot of unit tests in Python lately, and really took to liking the MagicMock module. This morning, I thought I'd take a crack to see if I could emulate much of its functionality, before I write a full-blown module for it. So far, it implements called(), call_count(), side_effect and return_value:

    The Module itself (./Test/MockSub.pm):

    package Test::MockSub;sub mock {my $self = bless {}, shift;my $sub = shift;%{ $self } = @_;if (defined $self->{return_value} && defined $self->{side_effect})+{die "use only one of return_value or side_effect";}my $called;{no strict 'refs';*$sub = sub {$self->{call_count} = ++$called;return $self->{return_value} if defined $self->{return_val+ue};$self->{side_effect}->() if $self->{side_effect};};}return $self;}sub called {return shift->call_count ? 1 : 0;}sub call_count {return shift->{call_count};}sub reset {my $self = shift;delete $self->{$_} for keys %{ $self };}1;

    The outer module I'm calling the inner module with mocked subs from (./Lanx.pm):

    package Lanx;use lib '.';use A;sub test {my $obj = A->new;$obj->foo;}1;

    The inner module I'm mocking (./A.pm):

    package A;sub new {return bless {}, shift;}sub foo {print "in A::foon";}1;

    ... and finally the script I'm testing it all from:

    use warnings;use strict;use feature 'say';use lib '.';use Test::MockSub;use Lanx;{# called() && call_counnt()my $a_foo = Test::MockSub->mock('A::foo');Lanx::test;Lanx::test;my $count = $a_foo->call_count;say "testing call_count(): $count";my $called = $a_foo->called;say "testing called(): $called";}{# return_valuemy $a_foo = Test::MockSub->mock('A::foo', return_value => 'True');my $ret = Lanx::test;say "testing return_value: $ret";}{# side_effectmy $cref = sub {die "thowing error";};my $a_foo = Test::MockSub->mock('A::foo', side_effect => $cref);eval{Lanx::test;};print "testing side_effect: ";say $@ ? 'success' : 'failed';}{# side_effect && return_value dies()my $a_foo;my $cref = sub {};eval{$a_foo = Test::MockSub->mock('A::foo', side_effect => $cref, r+eturn_value => 1);};print "testing side_effect & return_value dies(): ";say $@ ? 'success' : 'failed';}{# reset()my $a_foo = Test::MockSub->mock('A::foo', return_value => 1);my $ret1 = Lanx::test;$a_foo->reset;my $ret2 = Lanx::test;print "testing reset():";say defined $ret1 && ! defined $ret2 ? 'success' : 'failed';}

    There's Test::MockModule and Test::MockObject, but I like the built-in methods, and how I've done it allows for mocking functions, class methods and object methods all at the same time (at least I think).

    Thoughts, criticism and feedback welcome as always.

64. Glary Utilities
Complete Internet Repair does exactly what it says. It attempts to repair everything internet related, including networking problems.
55. Comodo Chromodo Private Internet Browser
Windows Defender Definition Updates for timely updating of your Windows Defender if the automatic update happens to fail.
52. AirDroid
Immunos is an extremely easy-to-work-with malware scanner that offers a straightforward interface and a powerful detection and removal system.
48. EMDB
WhatsApp Messenger for Android is a messaging app that uses your phone’s Internet connection (4G/3G/2G/EDGE or Wi-Fi, as available) to message and call friends and family.
Post Selected Items to:

Showing 10 items of about 55000

home  •   advertising  •   terms of service  •   privacy  •   about us  •   contact us  •   press release design by Popshop •   Official PR partner PRNews.io •   © 1999-2015 NewsKnowledge