Promise-Me
view release on metacpan or search on metacpan
which can be set to, respectively, "memory", "mmap" or "file"
shared_space_destroy
Boolean. Default to true. If true, the shared space used by the parent
and child processes will be destroy automatically. Disable this if you
want to debug or take a sneak peek into the data. The shared space will
be either shared memory of cache file depending on the value of
$SHARE_MEDIUM
use_async
This is a boolean value which is set automatically when a promise is
instantiated from "async".
It enables subroutine arguments to be passed to the code being run
asynchronously.
PRIVATE METHODS
_browse
Used for debugging purpose only, this will print out the PPI structure
of the code filtered and parsed.
_parse
After the code has been collected, this method will quickly parse it and
make changes to enable "async"
_reject_resolve
This is a common code called by either "resolve" or "reject"
_set_exit_values
This is called upon the exit of the asynchronous process to set some
general value about how the process exited.
See "exit_bit", "exit_signal" and "exit_status"
_set_shared_space
This is called in "exec" to share data including result between main
parent process and asynchronous process.
SHARED VARIABLES
It is important to be able to share variables between processes in a
seamless way.
When the asynchronous process is executed, the main process first fork
and from this point on all data is being duplicated in an impermeable
way so that if a variable is modified, it would have no effect on its
alter ego in the other process; thus the need for shareable variables.
You can enable shared variables in two ways:
1. declaring the variable as shared
my $name : shared;
# Initiate a value
my $location : shared = 'Tokyo';
# you can also use 'pshared'
my $favorite_programming_language : pshared = 'perl';
# You can share array, hash and scalar
my %preferences : shared;
my @names : shared;
2. calling "share"
my( $name, %prefs, @middle_names );
share( $name, %prefs, @middle_names );
Once shared, you can use those variables normally and their values will
be shared between the parent process and the asynchronous process.
For example:
my( $name, @first_names, %preferences );
share( $name, @first_names, %preferences );
$name = 'Momo Taro';
Promise::Me->new(sub
{
$preferences{name} = $name = 'Mr. ' . $name;
print( "Hello $name\n" );
$preferences{location} = 'Okayama';
$preferences{lang} = 'ja_JP';
$preferences{locale} = 'æ¡å¤ªé';
my $rv = $tbl->insert( \%$preferences )->exec || die( My::Exception->new( $tbl->error ) );
$rv;
})->then(sub
{
my $mail = My::Mailer->new(
to => $preferences{email},
name => $preferences{name},
body => $welcome_ja_file,
);
$mail->send || die( $mail->error );
})->catch(sub
{
my $exception = shift( @_ );
$logger->write( $exception );
})->finally(sub
{
$dbh->disconnect;
});
If you want to mix this feature and the usage of threads' "shared"
feature, use the keyword "pshared" instead of "shared", such as:
my $name : pshared;
Otherwise the two keywords would conflict.
SHARED MEMORY
This module uses shared memory using Module::Generic::SharedMemXS, or
shared cache file using Module::Generic::File::Cache if shared memory is
not supported, or if the value of the global package variable
$SHARE_MEDIUM is set to "file" instead of "memory". Alternatively you
can also have Promise::Me use cache mmap file by setting $SHARE_MEDIUM
to "mmap". This will have it use Module::Generic::File::Mmap, but note
that you will need to install Cache::FastMmap
The value of $SHARE_MEDIUM is automatically initialised to "memory" if
the system, on which this module runs, supports IPC::SysV, or "mmap" if
you have Cache::FastMmap installed, or else to "file"
Shared memory is used for:
1. shared variables
2. storing results returned by asynchronous processes
sleep(1);
$result .= "Peter ";
})->then(sub
{
print( "Promise 1: result is now: '$result'\n" );
});
my $p2 = Promise::Me->new(sub
{
sleep(0.5);
$result .= "John ";
})->then(sub
{
print( "Promise 2: result is now: '$result'\n" );
});
await( $p1, $p2 );
print( "Result is: '$result'\n" );
This will yield:
Promise 2: result is now: 'John '
Promise 1: result is now: 'John Peter '
Result is: 'John Peter '
CLASS VARIABLE
$RESULT_MEMORY_SIZE
This is the size in bytes of the shared memory block used for sharing
result between sub process and main process, such as when you call:
my $res = $prom->result;
It defaults to 512Kb
$SERIALISER
A string representing the serialiser to use by default. A serialiser is
used to serialiser data to share them between processes. This defaults
to "storable"
Currently supported serialisers are: CBOR::XS, Sereal and Storable
You can set accordingly the value for $SERIALISER to: "cbor", "sereal"
or "storable"
You can override this global value when you instantiate a new
Promise::Me object with the "serialiser" option. See "new"
Note that the serialiser used to serialise shared variable, is set only
via this class variable $SERIALISER
$SHARE_MEDIUM
The value of $SHARE_MEDIUM is automatically initialised to "memory" if
the system, on which this module runs, supports IPC::SysV, or "mmap" if
you have Cache::FastMmap installed, or else to "file"
$SHARED_MEMORY_SIZE
This is the size in bytes of the shared memory block used for sharing
variables between the main process and the sub processes. This is used
when you share variables, such as:
my $name : shared;
my( $name, %prefs, @middle_names );
share( $name, %prefs, @middle_names );
See "SHARED VARIABLES"
SERIALISATION
Promise::Me uses the following supported serialiser to serialise shared
data across processes:
* CBOR
* Sereal
* Storable
You can set which one to use globally by setting the class variable
$SERIALISER to "cbor", "sereal" or to "storable"
You can also set which serialiser to use on a per promise object by
setting the option "serialiser". See "new"
AUTHOR
Jacques Deguest <jack@deguest.jp>
SEE ALSO
Promise::XS, Promise::E6, Promise::AsyncAwait, AnyEvent::XSPromises,
Async, Promises, Mojo::Promise
Mozilla documentation on promises
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_pro
mises>
COPYRIGHT & LICENSE
Copyright(c) 2021-2022 DEGUEST Pte. Ltd. DEGUEST Pte. Ltd.
All rights reserved
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
( run in 0.905 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )