ハードウェア (Phidgets) コマンドを送信する Python で書いているプロジェクトがあります。複数のハードウェア コンポーネントとインターフェイスするため、複数のループを同時に実行する必要があります。
Pythonmultiprocessing
モジュールを調査しましたが、ハードウェアは一度に 1 つのプロセスでしか制御できないことが判明したため、すべてのループを同じプロセスで実行する必要があります。
Tk()
現時点では、実際に GUI ツールを使用せずに、ループを使用してタスクを実行できました。例えば:
from Tk import tk
class hardwareCommand:
def __init__(self):
# Define Tk object
self.root = tk()
# open the hardware, set up self. variables, call the other functions
self.hardwareLoop()
self.UDPListenLoop()
self.eventListenLoop()
# start the Tk loop
self.root.mainloop()
def hardwareLoop(self):
# Timed processing with the hardware
setHardwareState(self.state)
self.root.after(100,self.hardwareLoop)
def UDPListenLoop(self):
# Listen for commands from UDP, call appropriate functions
self.state = updateState(self.state)
self.root.after(2000,self.UDPListenLoop)
def eventListenLoop(self,event):
if event == importantEvent:
self.state = updateState(self.event.state)
self.root.after(2000,self.eventListenLoop)
hardwareCommand()
基本的に、ループを定義する唯一の理由は、同時にループする必要がある関数内でコマンドTk()
を呼び出せるようにするためです。root.after()
これは機能しますが、より良い/よりPythonicな方法はありますか? また、この方法が不要な計算オーバーヘッドを引き起こすのではないかと考えています (私はコンピューター サイエンスの専門家ではありません)。
ありがとう!