python parallel programming notes

Q: Operating 2 Gpio pins at same time

Abhilash WakodikarI want to write a program which can operate 2 gpio pins with different ON OFF time at same time. Say the time duration is 2 sec. In this time duration, I want to set GPIO 4 HIGH for 1 sec and LOW for 1 sec. And in that 2 sec same duration, I want to set GPIO 5 HIGH for 0.5 sec and LOW for 1.5 se…


Ah. you are asking a difficult question. The crux of the matter is that the “sleep” function is a “blocking” function, meaning that if say, you are executing two statements, first statement is “sleep(1)”, and the following statement is “sleep(2)”. Now when the python interpreter starts executing the first statement, it, I mean the lazy interpreter will actually sleeps for 1 second, before waking up to execute the following statement. / continue, ,,,
What you can can to execute two sequences of statement at the same time (“in parallel”, or “concurrently”) is to use the python module “multi-processing” (preferred) or “multi-threading” (not preferred).
If you wish to learn parallel or concurrent python programming, I would suggest to first read a tutorial about “multi-threading”, then move on to ‘multi-processing”. The reason that I say MP is preferred, and MT not preferred, but MT is the old thing which have been used for many many years. MP is new, sort of an improved version of MT, but unluckily many MP tutorials from time to time referred to the old MT. / to continue, …
In other words, you still need to know a little bit of MT (but don’t dig too deep) in order to thoroughly understand what the MP guys are talking. Just thinking aloud, sorry for the typos. Happy multi-processing. Cheers.
Ah, I forgot to ive your the important web link: (1) Python 3.8.1 Multiprocessing — Process-based parallelism docs.python.org/3/library/multiprocessing.html.
Abhilash Wakodikar
Abhilash Wakodikar
Can this be done with timer? I don’t know how to do programming of timer in raspberry pi but still asking.
14 hours later…

@Abhilash Wakodik, Yes, you can do with hardware and software timers. But #jspotla’s trick is the simplest for your application. In case you wish to have a timer solution for general cases, let me see if I can find more references. It might take some time, though. Cheers.

Now I have some timer functions for your reference. Let me see if I can listed them here.
# ftime73.py tlfong01 2019nov26hkt2022
# Rpi4B buster 2019sep26 python 3.7.3 Thronny 3.2

import inspect
from time import sleep
from datetime import datetime
import fprint171 as fprint

# ********************************************************************************
# ********************************************************************************

# *** Date Time Functions ***

def delaySeconds(secondsNum):

def oneSecondDelay():

(see full text)


I don’t know how to format my program listing. In case you find the above listing messed up, or cannot copy and paste to test run, you may try the following penzu file: penzu.com/p/739e7acd. The main thing is to import the python module “datetime”, then you can use the “elapsed time” thing to time two events.
There are two kinds of timers; (1) Counting up as a stop watch, to find elapsed time between tow events (a) start, (b), (2) Counting down as an alarm clock, where you give a start time, and let it count down to zero, as raise an alarm, to let Rpi switch off a LED etc. My file contains only counting up, you need to fiddle a bit to turn them to count down, by using a while loop, or “interrupts” which are more advanced, and not recommended to newbies. Happy programming. Cheers.
By the way, python multi-processing is not that hard as you expected. If you read the tutorial I suggested, or more other tutorials, and found them more suitable for you applications, then you may like to let me know you situation, and perhaps I can give some further suggestions.

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s