The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Async::ContextSwitcher - helps track execution context in async programs


This is a very simple module that helps you carry around execution context in async programs.

Idea is simple:

  • you create a "new" context for an entry point

    It can be a new web request, a new message from a queue to process or command line script command

  • use "cb_w_context" to create all callbacks in your application

  • correct context restored when your callbacks are called

  • use "context" to access data



Creates a new context and makes it the current one. Takes named pairs and stores them in the context.

    Async::ContextSwitcher->new( request => $psgi_env );


Returns the current context. Function is exported. Always returns context.

    my $ct = context->{request}{HTTP_CONTENT_TYPE};
    context->{user} = $user;


Wrapper for callbacks. Function is exported. Wraps a callback with code that stores and restores context to make sure correct context travels with your code.

    async_call( callback => cb_w_context { context->{good} = shift } );

Make sure that all callbacks in your code are created with this function or you can loose track of your context.


Ruslan Zakirov <>


Under the same terms as perl itself.