Skip Menu |

This queue is for tickets about the Syntax-Keyword-Dynamically CPAN distribution.

Report information
The Basics
Id: 133690
Status: resolved
Priority: 0/
Queue: Syntax-Keyword-Dynamically

People
Owner: Nobody in particular
Requestors: leonerd-cpan [...] leonerd.org.uk
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: 0.06
Fixed in: (no value)



Subject: dynamically set value not switched between concurrent `async sub`s
(attached script) gives unexpected output: $ perl two-task-switchers.pl [red] Started red [green] Started green [green] working [green] working [green] still working [green] still working [green] Finished red [TOP] Finished green Expected: $ perl two-task-switchers.pl [red] Started red [green] Started green [red] working [green] working [red] still working [green] still working [red] Finished red [green] Finished green -- Paul Evans
On Fri Nov 06 07:06:06 2020, PEVANS wrote: Show quoted text
> (attached script) gives unexpected output:
-- Paul Evans
Subject: rt133690.pl
#!/usr/bin/perl use v5.26; use warnings; use experimental 'signatures'; use Future::AsyncAwait; use Future::IO; use Syntax::Keyword::Dynamically; my $TRACER = bless { span => "TOP" }, "Tracer"; async sub subtask { await Future::IO->sleep( 0.1 ); $TRACER->emit( "working" ); await Future::IO->sleep( 0.1 ); $TRACER->emit( "still working" ); await Future::IO->sleep( 0.1 ); } async sub do_task ($name) { dynamically $TRACER->{span} = $name; $TRACER->emit( "Started $name" ); await subtask(); $TRACER->emit( "Finished $name" ); } Future->needs_all( do_task( "red" ), do_task( "green" ), )->get; sub Tracer::emit ($self, $message) { say "[$self->{span}] $message"; }
A better example to avoid dependency of Future::IO, making it more predicable and easier to insert Devel::MAT::Dumper et.al. to inspect things. -- Paul Evans
Subject: rt133690.pl
#!/usr/bin/perl use v5.26; use warnings; use experimental 'signatures'; use Future; use Future::AsyncAwait; use Syntax::Keyword::Dynamically; my $TRACER = bless { span => "TOP" }, "Tracer"; sub Tracer::emit ($self, $message) { say "[$self->{span}] $message"; } my @ticks; async sub subtask { my $f; push @ticks, $f = Future->new; await $f; $TRACER->emit( "working" ); push @ticks, $f = Future->new; await $f; } async sub do_task ($name) { dynamically $TRACER->{span} = $name; $TRACER->emit( "Started $name" ); await subtask(); $TRACER->emit( "Finished $name" ); } my $f = Future->needs_all( do_task( "red" ), do_task( "green" ), ); # Expecting two rounds say "D1"; $_->done foreach splice @ticks; say "D2"; $_->done foreach splice @ticks; $f->get;
On Sun Nov 08 13:09:31 2020, PEVANS wrote: Show quoted text
> A better example to avoid dependency of Future::IO, making it more > predicable and easier to insert Devel::MAT::Dumper et.al. to inspect > things.
Oops. Turns out this works fine as expected if you remember to use Syntax::Keyword::Dynamically -async; which isn't actually documented anywhere. Also, the choice of "-async" as a keyword collides with F-AA syntax if you load F-AA first: Expected a keyword to introduce a sub or sub-like construction at rt133690.pl line 10. Perhaps it would be best to enable this automatically somehow. Some careful thought needs to be had on module loading order though. -- Paul Evans
Partly a case of "operator error", by not specifying "-async" in the test case. But also partly a case of bad design, because that a) wasn't documented, and b) shouldn't really be necessary. The next release of F-AA (0.46) adds a new mechanism that S-K-D can use to enable async mode automatically, avoiding the user needing to specify it. Next version of S-K-D (0.07) will make use of it. -- Paul Evans