Skip to content

Instantly share code, notes, and snippets.

@ugexe

ugexe/blog.md Secret

Created December 6, 2022 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugexe/e43c84a51505d5151fb8141cdd9694ae to your computer and use it in GitHub Desktop.
Save ugexe/e43c84a51505d5151fb8141cdd9694ae to your computer and use it in GitHub Desktop.
Prompt: Write an advent blog post about concurrency in the Raku programming language

Advent is a time for reflection and celebration, and for many Raku programmers, that means thinking about the exciting new developments in the language. One area that has seen significant growth in recent years is concurrency.

In Raku, concurrency is the ability of a program to run multiple tasks simultaneously. This allows Raku programs to make better use of modern multi-core processors and improve performance. Raku offers several different ways to achieve concurrency, each with its own strengths and weaknesses.

One of the simplest ways to introduce concurrency into a Raku program is with the start function. This function allows you to run a block of code in a new thread, allowing multiple tasks to run concurrently. For example, the following code uses start to run two tasks concurrently:

my $t1 = start {
    # Task 1 code goes here
};
my $t2 = start {
    # Task 2 code goes here
};

Another way to achieve concurrency in Raku is with the Supply and react functions. A Supply represents a stream of events, and the react function allows you to react to those events as they occur. This is useful for programs that need to handle many simultaneous events, such as a web server that needs to handle multiple incoming requests.

my $supply = Supply.interval(0.5);
react {
    whenever $supply -> $event {
        # Event handling code goes here
    }
}

Finally, Raku also offers support for actor-based concurrency with the Actor class. An actor is a concurrent object that can receive messages and respond to them. This allows you to write concurrent programs in a more object-oriented style.

class MyActor does Actor {
    method receive($message) {
        # Message handling code goes here
    }
}

Concurrency is an important part of modern programming, and Raku provides powerful tools to make it easy to use. As you celebrate the advent season, take some time to explore these concurrency features and see how they can improve your Raku programs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment