最終更新: llysfactoryll 2021年08月29日(日) 16:52:28履歴
なんとか、チャタリングする様子を見てみたいと思っているわけであるが、Pythonでマルチスレッドができるという情報を見つけたので、試してみる。
ThreadPoolExecutorというものを使ってみる。threadingでもよさそうであるが・・
今回は、プロセスをまたがるマルチスレッドは考えない。
threading.Lock()を使って、ロックもちゃんとかけられる。sleepはtimeを使ってかけられる。
pythonではインクリメント演算子が使えなかった。「count += 1」と書かないといけない。
「count--」はエラーになるのだが、「++count」は何故かエラーにならないので、ハマってしまた。
sw_count
#!/usr/bin/env python3 from concurrent.futures import ThreadPoolExecutor import RPi.GPIO as GPIO import threading import time def sw_count(): global count global lock GPIO.setmode(GPIO.BCM) GPIO.setup(3, GPIO.IN) last = sw = GPIO.input(3) while True: sw = GPIO.input(3) if sw != last: lock.acquire() count += 1 lock.release() last = sw count = 0 lock = threading.Lock() executor = ThreadPoolExecutor() executor.submit(sw_count) while True: time.sleep(3) lock.acquire() pcnt = count lock.release() print(pcnt/2)
Pythonのthreadingとmultiprocessingを完全理解
https://qiita.com/kaitolucifer/items/e4ace07bd8e11...
[Python] スレッドで実装する
https://qiita.com/tchnkmr/items/b05f321fa315bbce4f...
Pythonの並行処理を理解したい [マルチスレッド編]
https://zenn.dev/ryo_kawamata/articles/python-conc...
https://qiita.com/kaitolucifer/items/e4ace07bd8e11...
[Python] スレッドで実装する
https://qiita.com/tchnkmr/items/b05f321fa315bbce4f...
Pythonの並行処理を理解したい [マルチスレッド編]
https://zenn.dev/ryo_kawamata/articles/python-conc...
コメントをかく