Thread::Queue::Duplex

Thread-safe request/response queue with identifiable elements
Download

Thread::Queue::Duplex Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Publisher Name:
  • Dean Arnold
  • Publisher web site:
  • http://search.cpan.org/~darnold/

Thread::Queue::Duplex Tags


Thread::Queue::Duplex Description

Thread-safe request/response queue with identifiable elements Thread::Queue::Duplex is a mapped queue, similar to Thread::Queue, except that as elements are queued, they are assigned unique identifiers, which are used to identify responses returned from the dequeueing thread. This class provides a simple RPC-like mechanism between multiple client and server threads, so that a single server thread can safely multiplex requests from multiple client threads. Note that simplex versions of the enqueue methods are provided which do not assign unique identifiers, and are used for requests to which no response is required/expected.In addition, elements are inspected as they are enqueued/dequeued to determine if they are Thread::Queue::Queueable (aka TQQ) objects, and, if so, the onEnqueue() or onDequeue() methods are called to permit any additional class-specific marshalling/unmarshalling to be performed. Thread::Queue::Duplex (aka TQD) is itself a Thread::Queue::Queueable object, thus permitting TQD objects to be passed between threads.SYNOPSIS use Thread::Queue::Duplex; # # create new queue, and require that there be # registered listeners for an enqueue operation # to succeed, and limit the max pending requests # to 20 # my $q = Thread::Queue::Duplex->new(ListenerRequired => 1, MaxPending => 20); # # register as a listener # $q->listen(); # # unregister as a listener # $q->ignore(); # # wait for a listener to register # $q->wait_for_listener($timeout); # # change the max pending limit # $q->set_max_pending($limit); # # enqueue elements, returning a unique queue ID # (used in the client) # my $id = $q->enqueue("foo", "bar"); # # enqueue elements, and wait for a response # (used in the client) # my $resp = $q->enqueue_and_wait("foo", "bar"); # # enqueue elements, and wait for a response # until $timeout secs (used in the client) # my $resp = $q->enqueue_and_wait_until($timeout, "foo", "bar"); # # enqueue elements at head of queue, returning a # unique queue ID (used in the client) # my $id = $q->enqueue_urgent("foo", "bar"); # # enqueue elements at head of queue and wait for response # my $resp = $q->enqueue_urgent_and_wait("foo", "bar"); # # enqueue elements at head of queue and wait for # response until $timeout secs # my $resp = $q->enqueue_urgent_and_wait_until($timeout, "foo", "bar"); # # enqueue elements for simplex operation (no response) # returning the queue object # $q->enqueue_simplex("foo", "bar"); $q->enqueue_simplex_urgent("foo", "bar"); # # dequeue next available element (used in the server), # waiting indefinitely for an element to be made available # returns shared arrayref, first element is unique ID, # which may be undef for simplex requests # my $foo = $q->dequeue; # # dequeue next available element (used in the server), # returns undef if no element immediately available # otherwise, returns shared arrayref, first element is unique ID, # which may be undef for simplex requests # my $foo = $q->dequeue_nb; # # dequeue next available element (used in the server), # returned undef if no element available within $timeout # seconds; otherwise, returns shared arrayref, first # element is unique ID, which may be undef for simplex requests # my $foo = $q->dequeue_until($timeout); # # dequeue next available element (used in the server), # but only if marked urgent; otherwise, returns undef # my $foo = $q->dequeue_urgent(); # # returns number of items still in queue # my $left = $q->pending; # # maps a response for the # queued element identified by $id; # $q->respond($id, @list); # # tests for a response to the queued # element identified by $id; returns undef if # not yet available, else returns the queue object # my $result = $q->ready($id); # # returns list of available response ID's; # if list provided, only returns ID's from the list. # Returns undef if none available. # In scalar context, returns only first available; # Else a list of available IDs # my @ids = $q->available(); # # wait for and return the response for the # specified unique identifier # (dequeue_response is alias) # my $result = $q->wait($id); my $result = $q->dequeue_response($id); # # waits up to $timeout seconds for a response to # the queued element identified by $id; returns undef if # not available within $timeout, else returns the queue object # my $result = $q->wait_until($id, $timeout); # # wait for a response to the queued # elements listed in @ids, returning a hashref of # the first available response(s), keyed by id # my $result = $q->wait_any(@ids); # # wait upto $timeout seconds for a response to # the queued elements listed in @ids, returning # a hashref of the first available response(s), keyed by id # Returns undef if none available in $timeout seconds # my $result = $q->wait_any_until($timeout, @ids); # # wait for responses to all the queued # elements listed in @ids, returning a hashref of # the response(s), keyed by id # my $result = $q->wait_all(@ids); # # wait upto $timeout seconds for responses to # all the queued elements listed in @ids, returning # a hashref of the response(s), keyed by id # Returns undef if all responses not recv'd # in $timeout seconds # my $result = $q->wait_all_until($timeout, @ids); # # mark an existing request # $q->mark($id, 'CANCEL'); # # test if a request is marked # print "Marked for cancel!" if $q->marked($id, 'CANCEL'); # # cancel specific operations # my $result = $q->cancel(@ids); # # cancel all operations # my $result = $q->cancel_all(); # # test if specified request has been cancelled # my $result = $q->cancelled($id); # # (class-level method) wait for an event on # any of the listed queue objects. Returns a # list of queues which have events pending # my $result = Thread::Queue::Duplex->wait_any( , ); # # (class-level method) wait upto $timeout seconds # for an event on any of the listed queue objects. # Returns undef if none available in $timeout seconds, # otherwise, returns a list of queues with events pending # my $result = Thread::Queue::Duplex->wait_any_until( $timeout, , ); # # (class-level method) wait for events on all the listed # queue objects. Returns the list of queue objects. # my $result = Thread::Queue::Duplex->wait_all( , ); # # (class-level method) wait upto $timeout seconds for # events on all the listed queue objects. # Returns empty list if all listed queues do not have # an event in $timeout seconds, otherwise returns # the list of queues # my $result = Thread::Queue::Duplex->wait_all_until( $timeout, , ); Requirements: · Perl


Thread::Queue::Duplex Related Software