Skip to content

Commit

Permalink
Changed CLI threads polling into blocking reads, to reduce CPU usage
Browse files Browse the repository at this point in the history
  • Loading branch information
doegox committed Oct 1, 2023
1 parent 606ec02 commit 42b7155
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...

## [unreleased][unreleased]
- Changed CLI threads polling into blocking reads, to reduce CPU usage (@doegox)
- Added support for timestamped comments in CLI via `rem`, `;`, `%` or `#` (@doegox)
- Fixed watchdog trigger during `hw factory_reset` (@doegox)
- Added PyInstaller support for CLI client (@augustozanellato)
Expand Down
14 changes: 7 additions & 7 deletions software/script/chameleon_com.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import serial
import chameleon_status

# each thread is waiting for its data for 100 ms before looping again
THREAD_BLOCKING_TIMEOUT = 0.1

class NotOpenException(Exception):
"""
Expand Down Expand Up @@ -82,7 +84,7 @@ def open(self, port):
except Exception:
# not all serial support dtr, e.g. virtual serial over BLE
pass
self.serial_instance.timeout = 0 # do not block
self.serial_instance.timeout = THREAD_BLOCKING_TIMEOUT
# clear variable
self.send_data_queue.queue.clear()
self.wait_response_map.clear()
Expand Down Expand Up @@ -208,8 +210,6 @@ def thread_data_receive(self):
data_buffer.clear()
continue
data_position += 1
else:
time.sleep(0.001)

def thread_data_transfer(self):
"""
Expand All @@ -218,10 +218,10 @@ def thread_data_transfer(self):
"""
while self.isOpen():
# get a task from queue(if exists)
if self.send_data_queue.empty():
time.sleep(0.001)
try:
task = self.send_data_queue.get(block=True, timeout=THREAD_BLOCKING_TIMEOUT)
except queue.Empty:
continue
task = self.send_data_queue.get()
task_cmd = task['cmd']
task_timeout = task['timeout']
task_close = task['close']
Expand Down Expand Up @@ -262,7 +262,7 @@ def thread_check_timeout(self):
else:
# sync mode, set timeout flag
self.wait_response_map[task_cmd]['is_timeout'] = True
time.sleep(0.001)
time.sleep(THREAD_BLOCKING_TIMEOUT)

def make_data_frame_bytes(self, cmd: int, data: bytearray = None, status: int = 0) -> bytearray:
"""
Expand Down

0 comments on commit 42b7155

Please sign in to comment.