reactor: Support pause() command even when the reactor is not running

If the reactor isn't running then implement pause using the system
sleep command.  This simplifies the users of pause().

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-03-31 20:48:29 -04:00
parent 7c991399ac
commit 565861f680
2 changed files with 13 additions and 5 deletions

View File

@ -3,7 +3,7 @@
# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import select, math import select, math, time
import greenlet import greenlet
import chelper import chelper
@ -71,9 +71,17 @@ class SelectReactor:
return 0. return 0.
return min(1., max(.001, self._next_timer - self.monotonic())) return min(1., max(.001, self._next_timer - self.monotonic()))
# Greenlets # Greenlets
def _sys_pause(self, waketime):
# Pause using system sleep for when reactor not running
delay = waketime - self.monotonic()
if delay > 0.:
time.sleep(delay)
return self.monotonic()
def pause(self, waketime): def pause(self, waketime):
g = greenlet.getcurrent() g = greenlet.getcurrent()
if g is not self._g_dispatch: if g is not self._g_dispatch:
if self._g_dispatch is None:
return self._sys_pause(waketime)
return self._g_dispatch.switch(waketime) return self._g_dispatch.switch(waketime)
if self._greenlets: if self._greenlets:
g_next = self._greenlets.pop() g_next = self._greenlets.pop()

View File

@ -3,7 +3,7 @@
# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import logging, threading, time import logging, threading
import serial import serial
import msgproto, chelper, util import msgproto, chelper, util
@ -338,10 +338,10 @@ def arduino_reset(serialport, reactor):
# First try opening the port at 1200 baud # First try opening the port at 1200 baud
ser = serial.Serial(serialport, 1200, timeout=0) ser = serial.Serial(serialport, 1200, timeout=0)
ser.read(1) ser.read(1)
time.sleep(0.100) reactor.pause(reactor.monotonic() + 0.100)
# Then try toggling DTR # Then try toggling DTR
ser.dtr = True ser.dtr = True
time.sleep(0.100) reactor.pause(reactor.monotonic() + 0.100)
ser.dtr = False ser.dtr = False
time.sleep(0.100) reactor.pause(reactor.monotonic() + 0.100)
ser.close() ser.close()