Skip main navigation

What is concurrency?

One of Scratch’s most notable and useful features is its ability to execute multiple blocks of code simultaneously.
A cartoon illustration of a computer character confused and shrugging

One of Scratch’s most notable and useful features is its ability to execute multiple blocks of code simultaneously. This means that the computer doesn’t need to wait for one code block to finish running before it executes the next block.

This is known as concurrency. When you use Scratch you don’t need to worry about the order of the code blocks executing, you can just assume that they will all run at the same time.

Here’s an example of a programme displaying such concurrency:

Two block programs each start with a 'when flag clicked' block. The first program has a 'forever' block containing three other blocks: a 'wait 0.5 secs', block followed by a 'say "On"' block, followed by a 'wait 0.5 secs' block. The second program has a 'forever' block containing two blocks: a 'wait 1 secs' block, followed by a 'say "Off"' block.

When this programme is run, both blocks of code are executed simultaneously. This causes the sprite they are attached to to switch between saying “On” and “Off” every half a second.

The above Scratch program being run, the sprite says the words "On" and "Off" every half a second.

Most text-based programming languages do not allow the programmer to execute multiple parts of their programme in parallel with such ease. For this reason, it is necessary for students that are new to such languages to learn to rethink how such problems can be tackled.

An example of concurrency

A student who is starting to learn Python might try and create the above Scratch programme like this:

from time import sleep

while True:
 sleep(0.5)
 print("On")
 sleep(0.5)

while True:
 sleep(1)
 print("Off")

However, running this programme will cause On to be printed until the program is forcibly quit. Off never gets printed.

To replicate the Scratch programme so that it works in Python, the code needs to be condensed into a single loop.

from time import sleep

while True:
 sleep(0.5)
 print("On")
 sleep(0.5)
 print("Off")

The above Python program being run, the words "On" and "Off" are printed every half a second.

This can sometimes be a struggle for students to understand, so it is an important concept to tackle fairly early on when they transition to writing programmes in a text-based language.

Are there any other examples you can use with learners to introduce the concept of concurrency in Python?

This article is from the free online

Scratch to Python: Moving from Block- to Text-based Programming

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education