Skip to content

Commit

Permalink
Use ptyprocess with hcitool subprocess (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu committed May 30, 2019
1 parent 2195954 commit 5deed32
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Changelog

### [Unreleased]
* Fix hcitool subprocess closing

## [0.12.0] - 2019-02-15
* Changed RuuviTagReactive's time-value from local to UTC
Expand Down
19 changes: 2 additions & 17 deletions ruuvitag_sensor/ble_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,14 @@ def start(bt_device=''):
DEVNULL = subprocess.DEVNULL if sys.version_info >= (3, 3) else open(os.devnull, 'wb')

subprocess.call('sudo hciconfig %s reset' % bt_device, shell=True, stdout=DEVNULL)
hcitool = subprocess.Popen(['sudo', '-n', 'hcitool', 'lescan', '--duplicates'], stdout=DEVNULL)
hcitool = ptyprocess.PtyProcess.spawn(['sudo', '-n', 'hcitool', 'lescan', '--duplicates'])
hcidump = ptyprocess.PtyProcess.spawn(['sudo', '-n', 'hcidump', '--raw'])
return (hcitool, hcidump)

@staticmethod
def stop(hcitool, hcidump):
# import psutil here so as long as all implementations are in the same file, all will work
import psutil

log.info('Stop receiving broadcasts')

def kill_child_processes(parent_pid):
try:
parent = psutil.Process(parent_pid)
except psutil.NoSuchProcess:
return

for process in parent.children(recursive=True):
subprocess.call(['sudo', '-n', 'kill', '-s', 'SIGINT', str(process.pid)])

kill_child_processes(hcitool.pid)
subprocess.call(['sudo', '-n', 'kill', '-s', 'SIGINT', str(hcitool.pid)])
kill_child_processes(hcidump.pid)
hcitool.close()
hcidump.close()

@staticmethod
Expand Down

0 comments on commit 5deed32

Please sign in to comment.