Python不断刷新变量
Python constantly refresh variable
我不知道这是不是一个愚蠢的问题,但我真的在努力解决这个问题。
我正在使用 obd 库进行编码。
现在我的问题是不断实现我的变量。
例如,一个变量输出汽车的实际速度。
该变量必须每秒或每 2 秒更新一次。要进行此更新,我必须 运行 2 行代码
cmd = obd.commands.RPM
rpm = connection.query(cmd)
但我必须在某些 while 循环和 if 语句中检查 rpm 变量。 (实时)
有机会完成这件事吗? (另一个 class 或线程或其他东西)它真的会帮助我在我的编程项目中取得飞跃。
谢谢:)
使用异步接口而不是 OBD:
Since the standard query() function is blocking, it can be a hazard for UI event loops. To deal with this, python-OBD has an Async connection object that can be used in place of the standard OBD object. Async is a subclass of OBD, and therefore inherits all of the standard methods. However, Async adds a few in order to control a threaded update loop. This loop will keep the values of your commands up to date with the vehicle. This way, when the user querys the car, the latest response is returned immediately.
The update loop is controlled by calling start() and stop(). To subscribe a command for updating, call watch() with your requested OBDCommand. Because the update loop is threaded, commands can only be watched while the loop is stoped.
import obd
connection = obd.Async() # same constructor as 'obd.OBD()'
connection.watch(obd.commands.RPM) # keep track of the RPM
connection.start() # start the async update loop
print connection.query(obd.commands.RPM) # non-blocking, returns immediately
http://python-obd.readthedocs.io/en/latest/Async%20Connections/
我不知道这是不是一个愚蠢的问题,但我真的在努力解决这个问题。
我正在使用 obd 库进行编码。 现在我的问题是不断实现我的变量。 例如,一个变量输出汽车的实际速度。 该变量必须每秒或每 2 秒更新一次。要进行此更新,我必须 运行 2 行代码
cmd = obd.commands.RPM
rpm = connection.query(cmd)
但我必须在某些 while 循环和 if 语句中检查 rpm 变量。 (实时)
有机会完成这件事吗? (另一个 class 或线程或其他东西)它真的会帮助我在我的编程项目中取得飞跃。
谢谢:)
使用异步接口而不是 OBD:
Since the standard query() function is blocking, it can be a hazard for UI event loops. To deal with this, python-OBD has an Async connection object that can be used in place of the standard OBD object. Async is a subclass of OBD, and therefore inherits all of the standard methods. However, Async adds a few in order to control a threaded update loop. This loop will keep the values of your commands up to date with the vehicle. This way, when the user querys the car, the latest response is returned immediately.
The update loop is controlled by calling start() and stop(). To subscribe a command for updating, call watch() with your requested OBDCommand. Because the update loop is threaded, commands can only be watched while the loop is stoped.
import obd connection = obd.Async() # same constructor as 'obd.OBD()' connection.watch(obd.commands.RPM) # keep track of the RPM connection.start() # start the async update loop print connection.query(obd.commands.RPM) # non-blocking, returns immediately
http://python-obd.readthedocs.io/en/latest/Async%20Connections/