0

I have two python scripts, for arguments sake lets call them A and B. Both A and B contain a while loop which runs indefinitely. They show live feeds of two different data collection devices. Script A is able to accept key presses. Now, what I would like to do is for script B to identify a keypress in script A. Each script is executed in a different terminal.

How am I able to communicate as fast as possible between the two python scripts? How am I able to pass a string or something similar from script A to script B without using a dummy file? With a dummy file I am regarding to a file which is constantly read by script B and written to by script A when a keypress has been registered. Can someone point me in the right direction?

Both scripts are showing live feeds of data using the while loops. It is therefore very important that neither script is stalling or waiting for something.

Script A:

import sys
import numpy
import select

def heardEnter():
  i, o, e = select.select([sys.stdin], [], [], 0.0001)
  for s in i:
    if s == sys.stdin:
      input = sys.stdin.readline()
      return True
  return False


while True:  
  if heardEnter():
    print "A: Pressed enter"

Script B:

while True:
  A_press_enter = None
  if A_press_enter == "Enter":
    print "B: Enter was pressed in script A"
The Dude
  • 3,795
  • 5
  • 29
  • 47
  • 1
    Why can you not put these both into one script, and perhaps use a thread? Do they really have to be in separate terminals? – Dr Xorile Sep 09 '15 at 23:31
  • your question is too broad, you could use any [IPC method](https://en.wikipedia.org/wiki/Inter-process_communication) e.g., if both scripts use `select()` already then it might be convenient to use a socket or a pipe for communication. – jfs Sep 10 '15 at 14:43

3 Answers3

1

if you really need to do it like this you will need a data transfer mechanism ... this could just be a simple json file or a redis database server....

basically one script writes its output to a file the other script scans that file (periodically)

theres no code for this answer .... sorry

a better idea would be to use threads or multiprocessing

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
1

Why not use a shared in-memory database between the two scripts?

You could simply use sqlite3, which support shared in-memory databases and is already included in the Python's standard library, or maybe Redis (http://redis.io/) if you want to try something fancier.

EDIT: in-memory databases in sqlite3 can only be shared between threads, not processes: https://www.sqlite.org/sharedcache.html

So as Joran Beasley pointed out, if you need to share between process, Redis could be your friend!

Dim'
  • 518
  • 6
  • 12
0

One options would be to use an environmental variable. See here.

Then in script A you write something to an environmental variable, and in script B you check for it. You'll need to consider the cases of a race condition, so that, for example B may be checking for a variable that does not yet exist. If you make it exist before the while loop in B, then you may over-write a valid key press from A before it is detected by B.

These conditions mainly exist during the start up, so you could, for example, always start one of the scripts first, or not press a button until both scripts are running.

Community
  • 1
  • 1
Dr Xorile
  • 967
  • 1
  • 7
  • 20
  • 2
    I dont think this would work ... as python runs in a subshell ... those changes would only be visible in processes that python instance started ... – Joran Beasley Sep 09 '15 at 23:41