本文整理汇总了Python中threading.Event.is_set方法的典型用法代码示例。如果您正苦于以下问题:Python Event.is_set方法的具体用法?Python Event.is_set怎么用?Python Event.is_set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.Event
的用法示例。
在下文中一共展示了Event.is_set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PWCounter
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class PWCounter(object): # PowerCounter class
def __init__(self, start_count, end_count, function, step=1):
self.start_count = start_count
self.end_count = end_count
self.function = function
self.finished = Event()
if self.start_count <= self.end_count:
self.step = -step
self.remaining_counts = self.end_count - self.start_count
elif self.start_count > self.end_count:
self.step = step
self.remaining_counts = self.start_count - self.end_count
self.function_triggered = Event()
def update(self):
if not self.finished.is_set():
self.remaining_counts += self.step
if self.remaining_counts <= 0:
self.finished.set()
elif self.finished.is_set() and not self.function_triggered.is_set():
self.function()
self.function_triggered.set()
def restart(self):
self.function_triggered.clear()
self.finished.clear()
if self.start_count <= self.end_count:
self.remaining_counts = self.end_count - self.start_count
elif self.start_count > self.end_count:
self.remaining_counts = self.start_count - self.end_count
示例2: RepeatTimer
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class RepeatTimer(Thread):
"""This class provides functionality to call a function at regular
intervals or a specified number of iterations. The timer can be cancelled
at any time."""
def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
Thread.__init__(self)
self.interval = interval
self.function = function
self.iterations = iterations
self.args = args
self.kwargs = kwargs
self.finished = Event()
self.daemon = True
def run(self):
count = 0
while not self.finished.is_set() and (self.iterations <= 0
or count < self.iterations):
self.finished.wait(self.interval)
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
count += 1
def cancel(self):
self.finished.set()
示例3: Worker
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class Worker(Thread):
def __init__(self, port=5557, local=False):
self._address = 'tcp://{}:{}'.format('127.0.0.1' if local else '0.0.0.0', port)
self._closed = Event()
super(Worker, self).__init__(target=self._serve)
self._close_msg = 'CLOSE'
def _serve(self):
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind(self._address)
while not self._closed.is_set():
i = socket.recv_pyobj()
if self._closed.is_set() and i == self._close_msg:
break
socket.send_pyobj(produce(i))
socket.close()
def close(self):
self._closed.set()
context = zmq.Context()
with closing(context.socket(zmq.REQ)) as socket:
socket.connect(self._address)
socket.send_pyobj(self._close_msg)
示例4: StubCollector
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class StubCollector(AbstractCollector):
"""Stub collector to inspect server behavior"""
def __init__(self, data=None, frequency=1.0):
super(StubCollector, self).__init__()
self.data = data
self.max_items = None
self.frequency = float(frequency)
self._shutdown = Event()
self._running = Event()
self.queued_data = []
def start(self):
self._running.set()
while not self._shutdown.is_set():
if self.max_items is None or self.max_items > len(
self.queued_data):
self.queue.put(self.data)
self.queued_data.append(self.data)
sleep(random() / self.frequency)
self._running.clear()
def wait_until_queuing_requests(self, timeout=None):
pass
def shutdown(self):
self._shutdown.set()
def is_queuing_requests(self):
return self._running.is_set()
def wait_until_shutdown(self, timeout=None):
self._shutdown.wait(timeout)
示例5: RepeatTimer
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class RepeatTimer(Thread):
'''Timer with custom number of iterations'''
def __init__(self, interval, function, iterations=0, args=None, kwargs=None):
Thread.__init__(self)
self.interval = interval
self.function = function
self.iterations = iterations
if args is None:
args = []
self.args = args
if kwargs is None:
kwargs = {}
self.kwargs = kwargs
self.finished = Event()
def run(self):
count = 0
while not self.finished.is_set() and (self.iterations <= 0 or count < self.iterations):
self.finished.wait(self.interval)
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
count += 1
def cancel(self):
self.finished.set()
示例6: RepeatTimer
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class RepeatTimer(Thread):
def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
Thread.__init__(self)
self.interval = interval
self.function = function
self.iterations = iterations
self.args = args
self.kwargs = kwargs
self.finished = Event()
def run(self):
count = 0
offset = 0
while not self.finished.is_set() and (self.iterations <= 0 or count < self.iterations):
if count == 0:
dt = datetime.now()
secs = (ceil(dt) - dt).total_seconds()
else:
secs = self.interval - offset
self.finished.wait(secs)
if not self.finished.is_set():
t = time.time()
self.function(*self.args, **self.kwargs)
offset = time.time() - t
count += 1
def cancel(self):
self.finished.set()
示例7: BCWorkerMonitor
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class BCWorkerMonitor(Thread):
def __init__(self, job, workunit, prefs):
self.sigquit = Event()
self.workunit = workunit
self.prefs = prefs
self.job = job
self.started = datetime.datetime.now()
def run(self):
while not self.sigquit.is_set():
#monitor resource consumption of running container
datapoint = {
'timepoint':datetime.datetime.now() - self.started,
'cpu':0,
'memory':0,
'disk_usage':0,
'message':''
}
requests.post(self.prefs['api'] + self.job['datapoints'], datapoint)
if not self.sigquit.is_set():
time.sleep(100)
def quit(self):
self.sigquit.set()
示例8: PerpetualTimer
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class PerpetualTimer(Thread):
"""Call a function after a specified number of seconds:
t = PerpetualTimer(30.0, f, args=[], kwargs={})
t.start()
t.cancel() # stop the timer's action if it's still waiting
"""
def __init__(self, interval, function, args=[], kwargs={}):
Thread.__init__(self)
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.finished = Event()
def cancel(self):
"""Stop the timer if it hasn't finished yet"""
self.finished.set()
def run(self):
self.function(*self.args, **self.kwargs)
while not self.finished.is_set():
try:
self.finished.wait(self.interval)
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
except (SystemExit, KeyboardInterrupt):
print('Cancel timer')
self.cancel()
示例9: MarkerThread
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class MarkerThread(Thread):
def __init__(self, da, zl, marker, coord, conf, pixDim):
Thread.__init__(self)
self.da = da
self.update = Event()
self.update.set()
self.__stop = Event()
self.zl = zl
self.marker = marker
self.coord = coord
self.conf = conf
self.pixDim = pixDim
self.img = self.marker.get_marker_pixbuf(zl)
def run(self):
while not self.__stop.is_set():
self.update.wait()
self.update.clear()
self.draw_markers()
def stop(self):
self.__stop.set()
self.update.set()
def draw_markers(self):
for string in self.marker.positions.keys():
if self.update.is_set() or self.__stop.is_set():
break
mpos = self.marker.positions[string]
if (self.zl <= mpos[2]) and (mpos[0], mpos[1]) != (self.coord[0], self.coord[1]):
gtk.threads_enter()
try:
self.da.draw_marker(self.conf, mpos, self.zl, self.img, self.pixDim, string)
finally:
gtk.threads_leave()
示例10: RepeatTimer
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class RepeatTimer(Thread):
def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
Thread.__init__(self)
self.interval = interval
self.function = function
self.iterations = iterations
self.args = args
self.kwargs = kwargs
self.finished = Event()
def run(self):
count = 0
global DOWNLOADED
while not self.finished.is_set() and (self.iterations <= 0 or count < self.iterations):
if not self.finished.is_set():
time1 = time.time()
self.function(*self.args, **self.kwargs)
time2 = time.time()
count += 1
diff = time2 - time1
if self.interval - diff > 0:
self.finished.wait(self.interval - diff)
def cancel(self):
self.finished.set()
示例11: myObj
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class myObj ():
def __init__(self, name, counter):
self._stopper = Event()
self.name = name
self.counter = counter
self.t = None
self.count = 0
def start(self):
self.t = Thread(name = self.name, target = self._doWork, args=(self.name, ) )
self.t.start()
def _doWork(self,threadName):
self._stopper.clear()
while not self._stopper.is_set():
print "%s: %s" % ( current_thread().getName(),time.ctime(time.time()) )
self.count += 1
self._stopper.wait(10)
print "Done"
print ("Count: %d"%self.count)
def stopit(self):
self._stopper.set()
self.t.join()
def stopped(self):
return self._stopper.is_set()
示例12: test_mock_pin_edges
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
def test_mock_pin_edges():
pin = Device.pin_factory.pin(2)
assert pin.when_changed is None
fired = Event()
pin.function = 'input'
pin.edges = 'both'
assert pin.edges == 'both'
pin.drive_low()
assert not pin.state
def changed():
fired.set()
pin.when_changed = changed
pin.drive_high()
assert pin.state
assert fired.is_set()
fired.clear()
pin.edges = 'falling'
pin.drive_low()
assert not pin.state
assert fired.is_set()
fired.clear()
pin.drive_high()
assert pin.state
assert not fired.is_set()
assert pin.edges == 'falling'
示例13: test_integration
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
def test_integration():
service_added = Event()
service_removed = Event()
type_ = "_http._tcp.local."
registration_name = "xxxyyy.%s" % type_
def on_service_state_change(zeroconf, service_type, state_change, name):
if name == registration_name:
if state_change is ServiceStateChange.Added:
service_added.set()
elif state_change is ServiceStateChange.Removed:
service_removed.set()
zeroconf_browser = Zeroconf()
browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change])
zeroconf_registrar = Zeroconf()
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_, registration_name,
socket.inet_aton("10.0.1.2"), 80, 0, 0,
desc, "ash-2.local.")
zeroconf_registrar.register_service(info)
try:
service_added.wait(1)
assert service_added.is_set()
zeroconf_registrar.unregister_service(info)
service_removed.wait(1)
assert service_removed.is_set()
finally:
zeroconf_registrar.close()
browser.cancel()
zeroconf_browser.close()
示例14: MainInterfaceAPI
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class MainInterfaceAPI(API):
def __init__(self, name, *di_args, **di_kwargs):
API.__init__(self, name, *di_args, **di_kwargs)
self._need_socket_event = Event()
self._running = Event()
def run(self):
while not self._running.is_set():
if not self._need_socket_event.is_set():
output = os.popen('ip route').read()
ip = self._find(output, "src (\d+\.\d+\.\d+\.\d+)", None)
if_name = self._find(output, "dev ([a-zA-Z]{3,4}\d)", None)
gateway = self._find(output, "default via (\d+\.\d+\.\d+\.\d+)", None)
if ip is not None:
self.interface_came_up(ip, if_name, if_name[0:-1] if not if_name is None else None, gateway)
self._running.wait(1)
def _find(self, _input, _format, default):
m = re.search(_format, _input, re.IGNORECASE)
if m:
return m.groups()[0]
return default
def socket_state_callback(self, socket, state):
if state == 0 or state == 11:
self._need_socket_event.set()
else:
self._need_socket_event.clear()
示例15: PeriodicTimer
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import is_set [as 别名]
class PeriodicTimer(Thread):
"""Call a function every /interval/ seconds:
t = PeriodicTimer(30.0, f, args=[], kwargs={})
t.start()
t.cancel()
"""
def __init__(self, interval, function, limit=None, finishCallback=None, args=[], kwargs={}):
Thread.__init__(self)
self.interval = interval
self.function = function
self.limit = limit
self.finishCallback = finishCallback
self.args = args
self.kwargs = kwargs
self.finished = Event()
def cancel(self):
"""Stop the timer if it hasn't finished yet"""
self.finished.set()
def run(self):
i = 0
while (not self.finished.is_set()) and (not self.limit or i < self.limit):
self.finished.wait(self.interval)
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
i += 1
if not self.finished.is_set() and self.limit and self.finishCallback:
self.finishCallback()