首页 > 解决方案 > 如何访问在另一个文件的线程中运行的文件的变量?

问题描述

我不能import,因为它会自动使线程文件运行并阻止主文件运行。我需要从客户端文件中访问一个变量。我正在为机器人编写这段代码,我真的需要能够从客户端访问变量。

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())

标签: pythonipcinterprocess

解决方案


推荐阅读