0

I want to share a variable between two already running scripts. Or even spit string from one script to another.

script_1.py:

sensor_status = "Nobody"
while 1:
    sensor_status = arduino.get_sensor_status()
    sleep(5)

script_2.py:

sensor_status = # realtime value from script_1
while 1:
    if sensor_status == "Nobody":
        pass
    else:
        alarm()
Kepler 186
  • 369
  • 2
  • 11
  • I think `multithreading` might work here – Sociopath Mar 26 '20 at 05:04
  • Does this answer your question? [Communication between two python scripts](https://stackoverflow.com/questions/16213235/communication-between-two-python-scripts) – Louis Etienne Mar 26 '20 at 05:06
  • 1
    You should add a `sleep()` to script 2 or it will run at 100% CPU. – Klaus D. Mar 26 '20 at 05:12
  • @Wizix Not really. The thing is, its more complex than that. It can be ran multiple times for different purposes. I can create a session file with an extension of .txt where both can access it but neither this or setting ports manual seems automated. Just hoping something quicker. – Kepler 186 Mar 26 '20 at 05:14
  • @Kepler186 Why dont't you call `script_1` from `script_2` by importing it in `script_2`? – Shubham Sharma Mar 26 '20 at 05:16
  • @ShubhamSharma I mean I could've also removed script_2 and proceed to execute alarm() whenever the condition is not satisfied. But that's not the idea, and again, the script is more complex than that, what I want is a way to communicate between two already running scripts. Is it possible? – Kepler 186 Mar 26 '20 at 05:20
  • 1
    Then can you use, as @Sociopath suggested, the ` multithreading` module and use a pipe or a queue ? – Louis Etienne Mar 26 '20 at 05:22
  • @Wizix Hmm. I shall look it up. But if you have any useful links, I would appreciate it. – Kepler 186 Mar 26 '20 at 05:24
  • 1
    Is the communication one-way or two-way? What kind of computer are the scripts running on? You could use the multiprocessing library. The docs have producer/consumer examples. You could also use something like MQTT. – RootTwo Mar 26 '20 at 05:26
  • 1
    Take a look at this article: [Introduction to Multiprocessing in Python](https://code.tutsplus.com/tutorials/introduction-to-multiprocessing-in-python--cms-30281) – Louis Etienne Mar 26 '20 at 05:30
  • @RootTwo / Wizix Beautiful. Multiprocessing's pipes seems the perfect solution. Thanks. – Kepler 186 Mar 26 '20 at 05:34

0 Answers0