Acme-Parataxis

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


```perl
use Acme::Parataxis;
# ... (See My::HTTP implementation in INTEGRATING SYNCHRONOUS MODULES) ...

Acme::Parataxis::run(sub {
    my $http = My::HTTP->new(verify_SSL => 0);
    my @urls = qw[http://example.com http://perl.org];

    # Spawn tasks for each URL
    my @futures = map {
        my $url = $_;
        Acme::Parataxis->spawn(sub { $http->get($url)->{status} })
    } @urls;

    # Collect results as they become ready
    say "Status for $urls[$_]: " . $futures[$_]->await( ) for 0..$#urls;
});
```

## Symmetric Producer/Consumer

builder/Acme/Parataxis/Builder.pm  view on Meta::CPAN

        say 'Building libparataxis...';
        require Affix::Build;    # This module is used for C compilation
        require Config;
        require File::Spec;
        my $arch_dir = catdir(qw[blib arch auto Acme Parataxis]);
        mkpath( $arch_dir, $verbose );
        my $build = Affix::Build->new(
            name  => 'parataxis',
            flags => {
                cflags => join( ' ',
                    ( $Config::Config{ccflags}, '-std=c11', map { '-I' . $_ } ( File::Spec->catdir( $Config::Config{archlibexp}, 'CORE' ), 'src' ) )
                ),
                ldflags => ( $^O eq 'MSWin32' ) ?
                    ( '-L' .
                        File::Spec->catdir( $Config::Config{archlibexp}, 'CORE' ) . ' -l' .
                        ( $Config::Config{libperl} =~ s/^lib//r =~ s/\.a$//r =~ s/\.lib$//r ) .
                        ' -lws2_32' ) : ( $^O eq 'darwin' ? '-undefined dynamic_lookup' : '' )
            },
            build_dir => $arch_dir,
            clean     => 0
        );

        # Add the C source file to be compiled. It's expected to be in 'lib/Acme/'
        $build->add('lib/Acme/Parataxis.c');
        say "Compiling and linking...";
        $build->compile_and_link();
        say "Build complete.";
        my %modules = map { $_ => catfile( 'blib', $_ ) } find( qr/\.pm$/,  'lib' );
        my %docs    = map { $_ => catfile( 'blib', $_ ) } find( qr/\.pod$/, 'lib' );
        my %scripts = map { $_ => catfile( 'blib', $_ ) } find( qr/(?:)/,   'script' );
        my %sdocs   = map { $_ => delete $scripts{$_} } grep {/.pod$/} keys %scripts;
        pm_to_blib( { %modules, %docs, %sdocs }, catdir(qw[blib lib auto]) );
        #
        mkpath( catdir(qw[blib script]), $verbose );
        for my $src ( keys %scripts ) {
            my $dest    = $scripts{$src};
            my $content = path($src)->slurp_raw;
            $content =~ s{^#!.*perl.*}{#!$^X};
            path($dest)->spew_raw($content);
            make_executable($dest);
        }
        #
        my %dist_shared   = map { $_ => catfile( qw[blib lib auto share dist],   $meta->name, abs2rel( $_, 'share' ) ) } find( qr/(?:)/, 'share' );
        my %module_shared = map { $_ => catfile( qw[blib lib auto share module], abs2rel( $_, 'module-share' ) ) } find( qr/(?:)/, 'module-share' );
        pm_to_blib( { %dist_shared, %module_shared }, catdir(qw[blib lib auto]) );
        mkpath( catdir(qw[blib arch]), $verbose );
        0;
    }
    method step_clean() { rmtree( $_, $verbose ) for qw[blib temp]; 0 }

    method step_install() {
        $self->step_build() unless -d 'blib';
        install(
            [   from_to           => $install_paths->install_map,
                verbose           => $verbose,
                dry_run           => $dry_run,
                uninstall_shadows => $uninst,
                skip              => undef,
                always_copy       => 1
            ]
        );
        0;
    }
    method step_realclean () { rmtree( $_, $verbose ) for qw[blib temp Build _build_params MYMETA.yml MYMETA.json]; 0 }

    method step_test() {
        $self->step_build() unless -d 'blib';
        require TAP::Harness::Env;
        require Config;
        my @libs = map { rel2abs( catdir( 'blib', $_ ) ) } qw[arch lib];
        local $ENV{PERL5LIB} = join( $Config::Config{path_sep}, @libs, ( defined $ENV{PERL5LIB} ? $ENV{PERL5LIB} : () ) );
        my %test_args = ( ( verbosity => $verbose ), ( jobs => $jobs ), ( color => -t STDOUT ), lib => [@libs], );
        TAP::Harness::Env->create( \%test_args )->runtests( sort map { $_->stringify } find( qr/\.t$/, 't' ) )->has_errors;
    }

    method get_arguments (@sources) {
        $_ = detildefy($_) for grep {defined} $install_base, $destdir, $prefix, values %{$install_paths};
        $install_paths = ExtUtils::InstallPaths->new( dist_name => $meta->name );
        return;
    }

    method Build(@args) {
        my $method = $self->can( 'step_' . $action );

lib/Acme/Parataxis.pod  view on Meta::CPAN

This example demonstrates how to perform multiple HTTP requests concurrently on a single interpretation thread.

    use Acme::Parataxis;
    # ... (See My::HTTP implementation in INTEGRATING SYNCHRONOUS MODULES) ...

    Acme::Parataxis::run(sub {
        my $http = My::HTTP->new(verify_SSL => 0);
        my @urls = qw[http://example.com http://perl.org];

        # Spawn tasks for each URL
        my @futures = map {
            my $url = $_;
            Acme::Parataxis->spawn(sub { $http->get($url)->{status} })
        } @urls;

        # Collect results as they become ready
        say "Status for $urls[$_]: " . $futures[$_]->await( ) for 0..$#urls;
    });

=head2 Symmetric Producer/Consumer



( run in 0.952 second using v1.01-cache-2.11-cpan-13bb782fe5a )