Fibers in Asynchronous Input and Output

You will learn how to use fibers in asynchronous input and output in this lesson.

Simplifying asynchronous input and output tasks

The call stack of a fiber can simplify asynchronous input and output tasks as well. As an example, let’s imagine a system where users sign on to a service by connecting to a server and providing their name, email, and age, in that order. This would be similar to the sign-on user flow of a website. To keep the example simple, instead of implementing an actual web service, let’s simulate user interactions using data entered on the command line. Let’s use the following simple sign-on protocol, where input data is highlighted:

  • hi: A user connects and a flow id is generated.

  • id data: The user of flow that corresponds to id enters the next expected data. For example, if the expected data for flow 42 is Alice, then the command for Alice would be 42 Alice.

  • bye: The program exits

For example, the following are the interactions of Alice and Bob, where the inputs to the simulation program are highlighted. Each user connects and then provides name, email, and age:

Get hands-on with 1400+ tech skills courses.