-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christian Sandberg
committed
Sep 19, 2016
1 parent
65ac211
commit 7cbdcf5
Showing
5 changed files
with
59 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import time | ||
import threading | ||
|
||
|
||
class SyncProducer(object): | ||
|
||
def __init__(self, network): | ||
self.network = network | ||
self.period = None | ||
self.transmit_thread = None | ||
self.stop_event = threading.Event() | ||
|
||
def transmit(self): | ||
self.network.send_message(0x80, []) | ||
|
||
def start(self, period=None): | ||
"""Start periodic transmission of SYNC message in a background thread.""" | ||
if period is not None: | ||
self.period = period | ||
|
||
if not self.period: | ||
raise ValueError("A valid transmission period has not been given") | ||
|
||
if not self.transmit_thread or not self.transmit_thread.is_alive(): | ||
self.stop_event.clear() | ||
self.transmit_thread = threading.Thread(target=self._periodic_transmit) | ||
self.transmit_thread.daemon = True | ||
self.transmit_thread.start() | ||
|
||
def stop(self): | ||
self.stop_event.set() | ||
if self.transmit_thread: | ||
self.transmit_thread.join(2) | ||
self.transmit_thread = None | ||
|
||
def _periodic_transmit(self): | ||
while not self.stop_event.is_set(): | ||
start = time.time() | ||
self.transmit() | ||
time.sleep(self.period - (time.time() - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters