Day 4: I4G 10 Days of Code Challenge

Photo by JJ Shev on Unsplash

Day 4: I4G 10 Days of Code Challenge

A very short, slightly chaotic lesson in asynchronous programming.

ยท

2 min read

The challenge for Day 4 was to ensure that three threads (functions happening separately) are executed in a particular order even if they are scheduled differently.

Easier put, it is analogous to ensuring that your friend waits for you to get some drinks before they start eating.

This is necessary because you have to account for the fact that maybe you'll spend a lot of time getting the drinks or you might not leave in time.

Asynchronous programming (which could be described as a subset of event driven programming) doesn't care who or which should come first "naturally". It just wants the program to follow an order it defines. It's a bit like that bossy friend that doesn't care if you're starving but enforces that you don't eat until they arrive (does this make sense?)

The Problem:

prob.png

THOUGHT PROCESS

I used a different language to solve this one (Python3) but my first idea was to pass each function (or event) as an argument to the next. Basically, a callback function.

What this means is that a condition for no2 to run is that no1 must have run and the same thing repeats with the rest of the functions.

An easier way to do this in Python 3 is to use the threading concept. This means that each thread is initialised as an event. The next event then has to wait for the previous event to be executed before it can be executed. I settled for this as a solution.

My Results

soln.png

Like I said, chaotic.๐Ÿ’…

ย