本文整理汇总了Python中pyee.EventEmitter类的典型用法代码示例。如果您正苦于以下问题:Python EventEmitter类的具体用法?Python EventEmitter怎么用?Python EventEmitter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EventEmitter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_asyncio_error
def test_asyncio_error():
"""Test that event_emitters can handle errors when wrapping coroutines as
used with asyncio.
"""
loop = new_event_loop()
ee = EventEmitter(loop=loop)
should_call = Future(loop=loop)
@ee.on('event')
async def event_handler():
raise PyeeTestError()
@ee.on('error')
def handle_error(exc):
should_call.set_result(exc)
async def create_timeout(loop=loop):
await sleep(0.1, loop=loop)
if not should_call.done():
raise Exception('should_call timed out!')
return should_call.cancel()
timeout = create_timeout(loop=loop)
@should_call.add_done_callback
def _done(result):
assert isinstance(result, PyeeTestError)
ee.emit('event')
loop.run_until_complete(gather(should_call, timeout, loop=loop))
loop.close()
示例2: __init__
def __init__(self, **kwargs):
EventEmitter.__init__(self)
for key, value in kwargs.iteritems():
setattr(self, key, value)
component_config = types.ComponentConfig(realm = u"sputnik")
wamp.ApplicationSessionFactory.__init__(self, config=component_config)
示例3: __init__
def __init__(self, url, debug=False):
WebSocketClient.__init__(self, url)
EventEmitter.__init__(self)
self.debug = debug
self._session = None
self._uniq_id = 0
self._callbacks = {}
示例4: __init__
def __init__(self, address):
EventEmitter.__init__(self)
threading.Thread.__init__(self)
self.sp = serial.Serial(address, timeout=1)
self.alive = True
self.lock = threading.Lock()
示例5: PendingPacket
class PendingPacket():
def __init__(self, packet, packet_sender):
print 'Init PendingPacket'
self.ee = EventEmitter()
self._packet_sender = packet_sender
self._packet = packet
def send(self):
def packet_send():
self._packetSender.send(self._packet)
self._intervalID = helpers.set_interval(
packet_send,
constants.TIMEOUT
)
self._packet_sender.send(self._packet)
def get_sequence_number(self):
return self._packet.get_sequence_number()
def acknowledge(self):
self._intervalID.cancel()
self.ee.emit('acknowledge')
示例6: Runner
class Runner(object):
def __init__(self, client, make_request):
self.client = client
self.make_request = make_request
self._pending = []
self.events = EventEmitter()
def _on_request_finished(self, _):
"""
Start the next waiting request if possible.
"""
if len(self.client.active) < self.client.max_clients:
self._start_request()
def _start_request(self):
"""
Start a request and add callbacks to its future.
Most notably, the completion of the requests's future will also trigger
the completion of a future we had prepared earlier.
"""
request = self.make_request(streaming_callback=lambda c: None)
future = concurrent.Future()
self.events.emit("request_ready", future, request)
self.client.fetch(request, callback=future.set_result)
self.events.emit("request_started", future)
self._pending.append(future)
future.add_done_callback(lambda f: self.events.emit("request_finished", f))
future.add_done_callback(self._pending.remove)
future.add_done_callback(self._on_request_finished)
示例7: __init__
def __init__(self, url, debug=False):
self.debug = debug
# by default socket connections don't timeout. this causes issues
# where reconnects can get stuck
# TODO: make this configurable?
socket.setdefaulttimeout(10)
WebSocketClient.__init__(self, url)
EventEmitter.__init__(self)
示例8: __init__
def __init__(self, id=None, secret=None, endpoint="https://api.coinsetter.com/v1"):
EventEmitter.__init__(self)
self.username = id
self.password = secret
self.endpoint = endpoint
self.session_id = None
from urllib2 import urlopen
self.ip = json.load(urlopen('http://jsonip.com'))['ip']
示例9: __init__
def __init__(self, log, clock, script_path, id, app, name, popen=subprocess.Popen):
EventEmitter.__init__(self)
self.log = log
self.clock = clock
self.script_path = script_path
self.app = app
self.name = name
self.runner = None
self._name = "%s-%s-%s" % (id, app, name)
self._popen = popen
示例10: test_emit_return
def test_emit_return():
ee = EventEmitter()
# make sure emission without callback retruns False
nt.assert_false(ee.emit('data'))
# add a callback
ee.on('data')(lambda: None)
# should return true now
nt.assert_true(ee.emit('data'))
示例11: __init__
def __init__(self):
"""Constructor."""
# run the superclass constructor
EventEmitter.__init__(self)
self.type = None
self.control_elements = []
self.data_conditions = []
# sensor_id should be unique
rndnum = random.randint(0, 100000)
self.sensor_id = "sensor" + str(rndnum)
示例12: RegistrationOnlyEmitter
class RegistrationOnlyEmitter(object):
def __init__(self):
self.emitter = EventEmitter()
def on(self, event, f):
if event in ['register_intent', 'register_vocab', 'recognizer_loop:utterance']:
self.emitter.on(event, f)
def emit(self, event, *args, **kwargs):
event_name = event.message_type
self.emitter.emit(event_name, event, *args, **kwargs)
示例13: IncomingMessage
class IncomingMessage(object):
def __init__(self, im_id, size):
self.log = logging.getLogger(
'%s' % self.__class__.__name__
)
self.log.debug('New IncomingMessage Created')
self.im_id = im_id
self.size = size
self.ee = EventEmitter()
self.synced = False
self._next_sequence_number = 0
self._sync_sequence_number = None
self._packets = SortedList()
self.body = ''
self.waiting = False
def add_to_body(self, payload):
if payload in self.body:
self.log.debug('This content is already in the body.')
return
else:
self.body += payload
def reset(self):
self.log.debug('IncomingMessage Reset')
self.log.debug('Self Packets: %s', self._packets)
self._packets.clear()
self.synced = False
self._next_sequence_number = 0
self._sync_sequence_number = None
try:
self.log.debug('Downloaded (%s) | Total Size (%s)', len(self.body), self.size)
if len(self.body) >= int(self.size):
self.log.debug('Download Complete')
if len(self.body) > int(self.size):
self.log.debug('Oversized Message')
self.ee.emit('complete', {'body': self.body})
return
else:
# print self._message, self._message_size
self.log.debug('Still downloading...')
self.waiting = True
except Exception as e:
self.log.debug('Problem with resetting IncomingMessage: %s', e)
示例14: test_emit_error
def test_emit_error():
ee = EventEmitter()
with nt.assert_raises(Exception):
ee.emit('error')
@ee.on('error')
def onError():
pass
# No longer raises and error instead return True indicating handled
nt.assert_true(ee.emit('error'))
示例15: RegistrationOnlyEmitter
class RegistrationOnlyEmitter(object):
def __init__(self):
self.emitter = EventEmitter()
def on(self, event, f):
allow_events_to_execute = True
if allow_events_to_execute:
# don't filter events, just run them all
print "Event: "+str(event)
self.emitter.on(event, f)
else:
# filter to just the registration events,
# preventing them from actually executing
if event in [
'register_intent',
'register_vocab',
'recognizer_loop:utterance'
]:
print "Event: " + str(event)
self.emitter.on(event, f)
def emit(self, event, *args, **kwargs):
event_name = event.type
self.emitter.emit(event_name, event, *args, **kwargs)
def once(self, event, f):
self.emitter.once(event, f)
def remove(self, event_name, func):
pass