python - 如何访问在另一个文件的线程中运行的文件的变量?
问题描述
我不能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())
解决方案
推荐阅读
- python - 按百分比分组以计算非数值列值的权重
- java - Spring Boot 审计被无限调用
- javascript - 从 JS 计算器中的两个输入中获取值
- sql - 如何从字段中检索一定百分比的订单?
- r - R 库“XML”无法识别编码
- python-3.x - 改变单个字符颜色的最简单方法是什么?
- neo4j - 如何在社区 Neo4j JAVA Bolt Driver Api 中应用查询超时
- javascript - 数据表服务器端 .draw() 请求成功但不更新
- reactjs - How to write unit test for Material UI slider? "Error: Cannot read property 'addEventListener' of null" thrown when attempting to render component
- swagger - 在 Java/Kotlin 的 Swagger/Micronaut API 中重用 @Parameter 文档