Ayushman21
Posts: 1
Joined: Sat Apr 17, 2021 7:18 am

Running 2 codes simultaneously

Sat Apr 17, 2021 7:23 am

Hi,
I am currently using my raspberry pi 4B for a project where in i have written two sets of code one to run the motors of a rover I built and another code which allows me to live stream via the pi cam.

However I am unable to run both these programs simultaneously. They are only working seperately.

I don't want to run them on start up but want to fun them both together so that not only can i drive my river but also see through the live stream.

Can anyone help me as to how i should get it done?
I am using MU IDE.

Thank you

Heater
Posts: 18032
Joined: Tue Jul 17, 2012 3:02 pm

Re: Running 2 codes simultaneously

Sat Apr 17, 2021 1:24 pm

We don't care what IDE you are using. I have never heard of MU IDE.

More important is the programming language you are using. Which I gather is Python.

One way to combine two sets of code into one is to use threads: https://realpython.com/intro-to-python-threading/. Run each of your codes in a separate thread.

Another way to combine two sets of codes is to use asynchronous programming, async: https://realpython.com/python-async-features/. Asynchronous tasks are like threads in that a separate async task can run each part of the code. The will like need to be some little changes to each of your codes to get them running asynchronously in as a single program.

Or, quite often is is possible to combine functionality from two programs into one by reorganising the way your code is structured. Consider a simple case where program "A" has a loop and does "workA" in the loop, another program "B" has a loop and does "workB" in the loop. These could be combined into a single program "C" that has a loop that does both "workA" and workB in it's loop body.

Which of these techniques is most suitable depends on what your code does. You will have to decide.
Memory in C++ is a leaky abstraction .

klricks
Posts: 7738
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Running 2 codes simultaneously

Sat Apr 17, 2021 2:31 pm

Ayushman21 wrote:
Sat Apr 17, 2021 7:23 am
...
I am using MU IDE.
You should be able to start many programs at near the same time from the command line.
You are not trying to run your code from the IDE are you? As mentioned the IDE that you used to create your code is irrelevant as the IDE is normally not used to run code once the code is written and working.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

User avatar
B.Goode
Posts: 11889
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Running 2 codes simultaneously

Sat Apr 17, 2021 3:44 pm

Heater wrote:
Sat Apr 17, 2021 1:24 pm
We don't care what IDE you are using. I have never heard of MU IDE.


I agree that the choice of Editor or IDE is irrelevant to executing production code.

But for reference, the Mu Python editor and IDE is well known to Raspberry Pi themselves -

https://www.raspberrypi.org/blog/mu-python-ide/

https://projects.raspberrypi.org/en/pro ... ed-with-mu

At one stage I believe it was the default choice of the Raspberry Pi education team for use in their PiCademy CPD sessions for teachers.

User avatar
B.Goode
Posts: 11889
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Running 2 codes simultaneously

Sat Apr 17, 2021 3:46 pm

Ayushman21 wrote:
Sat Apr 17, 2021 7:23 am
Hi,
I am currently using my raspberry pi 4B for a project where in i have written two sets of code one to run the motors of a rover I built and another code which allows me to live stream via the pi cam.

However I am unable to run both these programs simultaneously. They are only working seperately.

I don't want to run them on start up but want to fun them both together so that not only can i drive my river but also see through the live stream.

Can anyone help me as to how i should get it done?
I am using MU IDE.

Thank you

Could it be as simple as, at the shell (command line) prompt:

Code: Select all

./rover_script &
./camera_script &


Of course, if the point of your post is that there is some clash or conflict of resources that prevents them both running at the same time then you will need to resolve that conflict first.

Heater
Posts: 18032
Joined: Tue Jul 17, 2012 3:02 pm

Re: Running 2 codes simultaneously

Sat Apr 17, 2021 3:59 pm

Ah yes, I overlooked that option, keep the programs as they are and run them both as independent process as normal.

One could do this from a third Python program that starts the two original programs using "os.system" or some other technique: https://www.tutorialspoint.com/How-can- ... un-another

Or just have program "A" start program "B" as part of its start up.
Memory in C++ is a leaky abstraction .

W. H. Heydt
Posts: 14655
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Running 2 codes simultaneously

Sat Apr 17, 2021 5:08 pm

OP probably needs to run the command program in the foreground (so he can interact with it), while the video stream can run in the background.

So...

Code: Select all

web-stream &
control-prog

Return to “Python”