0

I can't import because it automatically makes the threaded file run and that stops the main file from running. I need to access a variable from the client file. I am writing this code for a robot and I really need to be able to access the variables from the client.

import serial
import serial.tools.list_ports as list_ports
import time
import threading
from subprocess import call

x = ""
def thread_second():
  call(["python3", "client.py"])
processThread = threading.Thread(target=thread_second)
processThread.start()

def getch():
  import sys, tty, termios
  old_settings = termios.tcgetattr(0)
  new_settings = old_settings[:]
  new_settings[3] &= ~termios.ICANON
  try:
    termios.tcsetattr(0, termios.TCSANOW, new_settings)
    ch = sys.stdin.read(1)
  finally:
    termios.tcsetattr(0, termios.TCSANOW, old_settings)
  return ch


if __name__ == '__main__':

    port_data = list_ports.comports()
    ser = serial.Serial("/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_758333530353514112B1-if00", 115200, timeout=0)
    ser.flush()
    print(port_data)
    while True:
        previous = x
        x = getch()
        x = x + "\n"
        ser.write(x.encode())
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    In general you can't access anything in another process. – Barmar Jul 30 '20 at 17:44
  • Related:[How to share variables across scripts in python?](https://stackoverflow.com/questions/1829116/how-to-share-variables-across-scripts-in-python), [Passing variables between two python processes](https://stackoverflow.com/questions/41844145/passing-variables-between-two-python-processes), [Passing data between separately running Python scripts](https://stackoverflow.com/questions/43861164/passing-data-between-separately-running-python-scripts). -Keep the data in a file,pickle,json,xml,database...; - send data back and forth via pipes,queues sockets... – wwii Jul 30 '20 at 18:03
  • [https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes](https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes) – wwii Jul 30 '20 at 18:07

0 Answers0