arnauinsti21
Posts: 15
Joined: Mon Aug 06, 2018 10:52 am

Passing variables

Wed Aug 22, 2018 5:49 pm

Hey everyone!

I need help with a project.
I need to pass variables between multiple running python scripts in real time.

Let's say I have two scripts, and in the second one I have the x variable, but this variable changes constantly; I need to know its value at every moment, because (let's say) the first script has to blink a led when x reaches a certain number. But x has to be in the second script.

How could I do that?
I do really need help, thank you very much!

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Passing variables

Wed Aug 22, 2018 5:54 pm

Sounds like a job for shared memory.

Easy enough to do in C. Not sure if/how to do it in Python, but it should be possible.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

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

Re: Passing variables

Wed Aug 22, 2018 5:57 pm

Haven't we got you your place at University yet... ?

Is it too late to suggest that you could side-step this issue by just having one script not two?

You could write the data out to a temporary file and read it back from the other script.

arnauinsti21
Posts: 15
Joined: Mon Aug 06, 2018 10:52 am

Re: Passing variables

Wed Aug 22, 2018 7:05 pm

B.Goode wrote:
Wed Aug 22, 2018 5:57 pm
Haven't we got you your place at University yet... ?

Is it too late to suggest that you could side-step this issue by just having one script not two?

You could write the data out to a temporary file and read it back from the other script.
The thing is that I need to do a couple of (while True) loops.
A while true loop in a script that tells me the reading of an IR reciever, a while true loop in another script with analize the light intensity given by an LDR, a while true loop that controlls a gas sensor...

How can I do this in just one script?? Trust me I would love to use only a script.

Thanks for answering, I do really apreciate your help.

arnauinsti21
Posts: 15
Joined: Mon Aug 06, 2018 10:52 am

Re: Passing variables

Wed Aug 22, 2018 7:09 pm

n67 wrote:
Wed Aug 22, 2018 5:54 pm
Sounds like a job for shared memory.

Easy enough to do in C. Not sure if/how to do it in Python, but it should be possible.
Thanks! I will check that out!
I still prefer python, but thanks for the tip ;)

hippy
Posts: 6692
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Passing variables

Wed Aug 22, 2018 7:30 pm

Memory Mapped files (mmap), Named Pipes, MQTT, sockets, TCP - Are some of the other ways to do IPC.

As to how to do it all in one program; threading.

arnauinsti21
Posts: 15
Joined: Mon Aug 06, 2018 10:52 am

Re: Passing variables

Thu Aug 23, 2018 8:35 am

hippy wrote:
Wed Aug 22, 2018 7:30 pm
Memory Mapped files (mmap), Named Pipes, MQTT, sockets, TCP - Are some of the other ways to do IPC.

As to how to do it all in one program; threading.
Thank you very much! I'll check the threading things. Thanks!!

Return to “Python”