'Have two functions work concurrently in the same python program?

I have a code that polls data from a website and displays it on a screen attached to a raspberry pi W model. The code is written in python on my raspberry pi.

My current code is the one below, the print command is used to display the results on the screen. The screen display is temporary and only shows the results for a few seconds, it doesn’t latch.

However since data collection takes a few minutes, the print command would be waiting for that and the screen is not displaying anything. I want the screen to always show something and avoid being blank while it waits for data collection part of the code.

#prog1.py
while i>0:

#data collection takes a few minutes to complete 
 a1="1" #collect data from website.com
 a2="2" #collect data from website.com
 a3="3" #collect data from website.com

#after data collection, print on screen 
 x=a1 #copy the results taken from website.com
 y=a2
 z=a3
 print(x) #print the results
 print(y)
 print(z)

My idea is to never have the screen wait for the data collection part of the code, so it would always display some results while the other code is collecting data.

Is it possible to do so in one program? Or would I need to split them into two programs and call the data collecting function into the display results on screen program?



Solution 1:[1]

In such a case I would use a multiprocessing.Process or threading.Thread to do the "data collection". They both use the same API. Let's call this the "worker".

Meanwhile the "main" process or thread runs in a loop that displays results, waits a while and checks if the data collection is finished. If it is, it updates the display.

The choice between Process or Thread depends on the context. If the "data collection" is e.g. mostly network traffic, you can safely use a Thread. If it involves a lot of computation, it's better to use a Process. The reason for this is that in CPython, only one thread at a time can be executing Python bytecodes.

One difference between Thread and Process is that the former shares all memory with the main (original) thread.

So the "worker" thread can just save the results and the main thread can access them. It would be prudent to protect those results with e.g. a Lock, so that the "worker" isn't in the process of updating the data while "main" is accessing them to display them.

If you use a Process, you will have to transmit the data back from the "worker" to the main process, using interprocess communication like e.g. a Queue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1