Python源码示例:twisted.internet.reactor.callInThread()
示例1
def test_callInThread(self):
"""
Test callInThread functionality: set a C{threading.Event}, and check
that it's not in the main thread.
"""
def cb(ign):
waiter = threading.Event()
result = []
def threadedFunc():
result.append(threadable.isInIOThread())
waiter.set()
reactor.callInThread(threadedFunc)
waiter.wait(120)
if not waiter.isSet():
self.fail("Timed out waiting for event.")
else:
self.assertEqual(result, [False])
return self._waitForThread().addCallback(cb)
示例2
def test_callFromThread(self):
"""
Test callFromThread functionality: from the main thread, and from
another thread.
"""
def cb(ign):
firedByReactorThread = defer.Deferred()
firedByOtherThread = defer.Deferred()
def threadedFunc():
reactor.callFromThread(firedByOtherThread.callback, None)
reactor.callInThread(threadedFunc)
reactor.callFromThread(firedByReactorThread.callback, None)
return defer.DeferredList(
[firedByReactorThread, firedByOtherThread],
fireOnOneErrback=True)
return self._waitForThread().addCallback(cb)
示例3
def test_wakerOverflow(self):
"""
Try to make an overflow on the reactor waker using callFromThread.
"""
def cb(ign):
self.failure = None
waiter = threading.Event()
def threadedFunction():
# Hopefully a hundred thousand queued calls is enough to
# trigger the error condition
for i in xrange(100000):
try:
reactor.callFromThread(lambda: None)
except:
self.failure = failure.Failure()
break
waiter.set()
reactor.callInThread(threadedFunction)
waiter.wait(120)
if not waiter.isSet():
self.fail("Timed out waiting for event")
if self.failure is not None:
return defer.fail(self.failure)
return self._waitForThread().addCallback(cb)
示例4
def test_callInThread(self):
"""
Test callInThread functionality: set a C{threading.Event}, and check
that it's not in the main thread.
"""
def cb(ign):
waiter = threading.Event()
result = []
def threadedFunc():
result.append(threadable.isInIOThread())
waiter.set()
reactor.callInThread(threadedFunc)
waiter.wait(120)
if not waiter.isSet():
self.fail("Timed out waiting for event.")
else:
self.assertEqual(result, [False])
return self._waitForThread().addCallback(cb)
示例5
def test_callFromThread(self):
"""
Test callFromThread functionality: from the main thread, and from
another thread.
"""
def cb(ign):
firedByReactorThread = defer.Deferred()
firedByOtherThread = defer.Deferred()
def threadedFunc():
reactor.callFromThread(firedByOtherThread.callback, None)
reactor.callInThread(threadedFunc)
reactor.callFromThread(firedByReactorThread.callback, None)
return defer.DeferredList(
[firedByReactorThread, firedByOtherThread],
fireOnOneErrback=True)
return self._waitForThread().addCallback(cb)
示例6
def test_wakerOverflow(self):
"""
Try to make an overflow on the reactor waker using callFromThread.
"""
def cb(ign):
self.failure = None
waiter = threading.Event()
def threadedFunction():
# Hopefully a hundred thousand queued calls is enough to
# trigger the error condition
for i in range(100000):
try:
reactor.callFromThread(lambda: None)
except:
self.failure = failure.Failure()
break
waiter.set()
reactor.callInThread(threadedFunction)
waiter.wait(120)
if not waiter.isSet():
self.fail("Timed out waiting for event")
if self.failure is not None:
return defer.fail(self.failure)
return self._waitForThread().addCallback(cb)
示例7
def __new__(cls, *args, **kw):
if not hasattr(cls, '_instance'):
instance = super(Controller, cls).__new__(cls)
instance._allocated_pins = {}
instance._poll_queue = select.epoll()
instance._available_pins = []
instance._running = True
# Cleanup before stopping reactor
reactor.addSystemEventTrigger('before', 'shutdown', instance.stop)
# Run the EPoll in a Thread, as it blocks.
reactor.callInThread(instance._poll_queue_loop)
cls._instance = instance
return cls._instance
示例8
def test_clean_running_threads(self):
import threading
import time
current_threads = list(threading.enumerate())
reactor = self.make_reactor()
timeout = self.make_timeout()
spinner = self.make_spinner(reactor)
spinner.run(timeout, reactor.callInThread, time.sleep, timeout / 2.0)
# Python before 2.5 has a race condition with thread handling where
# join() does not remove threads from enumerate before returning - the
# thread being joined does the removal. This was fixed in Python 2.5
# but we still support 2.4, so we have to workaround the issue.
# http://bugs.python.org/issue1703448.
self.assertThat(
[thread for thread in threading.enumerate() if thread.isAlive()],
Equals(current_threads))
示例9
def test_callInThread(self):
"""
Test callInThread functionality: set a C{threading.Event}, and check
that it's not in the main thread.
"""
def cb(ign):
waiter = threading.Event()
result = []
def threadedFunc():
result.append(threadable.isInIOThread())
waiter.set()
reactor.callInThread(threadedFunc)
waiter.wait(120)
if not waiter.isSet():
self.fail("Timed out waiting for event.")
else:
self.assertEquals(result, [False])
return self._waitForThread().addCallback(cb)
示例10
def test_callFromThread(self):
"""
Test callFromThread functionality: from the main thread, and from
another thread.
"""
def cb(ign):
firedByReactorThread = defer.Deferred()
firedByOtherThread = defer.Deferred()
def threadedFunc():
reactor.callFromThread(firedByOtherThread.callback, None)
reactor.callInThread(threadedFunc)
reactor.callFromThread(firedByReactorThread.callback, None)
return defer.DeferredList(
[firedByReactorThread, firedByOtherThread],
fireOnOneErrback=True)
return self._waitForThread().addCallback(cb)
示例11
def test_wakerOverflow(self):
"""
Try to make an overflow on the reactor waker using callFromThread.
"""
def cb(ign):
self.failure = None
waiter = threading.Event()
def threadedFunction():
# Hopefully a hundred thousand queued calls is enough to
# trigger the error condition
for i in xrange(100000):
try:
reactor.callFromThread(lambda: None)
except:
self.failure = failure.Failure()
break
waiter.set()
reactor.callInThread(threadedFunction)
waiter.wait(120)
if not waiter.isSet():
self.fail("Timed out waiting for event")
if self.failure is not None:
return defer.fail(self.failure)
return self._waitForThread().addCallback(cb)
示例12
def testWakerOverflow(self):
self.failure = None
waiter = threading.Event()
def threadedFunction():
# Hopefully a hundred thousand queued calls is enough to
# trigger the error condition
for i in xrange(100000):
try:
reactor.callFromThread(lambda: None)
except:
self.failure = failure.Failure()
break
waiter.set()
reactor.callInThread(threadedFunction)
waiter.wait(120)
if not waiter.isSet():
self.fail("Timed out waiting for event")
if self.failure is not None:
return defer.fail(self.failure)
示例13
def start(self):
if self.running:
return
self.log.debug('starting')
self.running = True
# Start monitoring the vcore grpc channel
reactor.callInThread(self.monitor_vcore_grpc_channel)
# Start monitoring logical devices and manage agents accordingly
reactor.callLater(0, self.monitor_logical_devices)
self.log.info('started')
return self
示例14
def render(self, request):
"""
Fulfill requests by forwarding them to snapd.
We use a synchronous implementation of HTTP over Unix sockets, so we do
the request in a worker thread and have it call request.finish.
"""
cors.config_cors(request)
reactor.callInThread(self.do_snapd_request, request)
return NOT_DONE_YET
示例15
def gotProtocol(self, p):
log.info('gotProtocol, connecting {name}', name=self.name)
self.protocol = p
#def later():
d = p.connect(self.name, keepalive=0, cleanStart=True)
d.addCallback(self.subscribe)
#d.addCallback(self.prepareToPublish)
#reactor.callLater(random.randint(2, 7), later)
#reactor.callInThread(later)
示例16
def callMultipleInThread(tupleList):
"""
Run a list of functions in the same thread.
tupleList should be a list of (function, argsList, kwargsDict) tuples.
"""
from twisted.internet import reactor
reactor.callInThread(_runMultiple, tupleList)
示例17
def testWakeUp(self):
# Make sure other threads can wake up the reactor
d = Deferred()
def wake():
time.sleep(0.1)
# callFromThread will call wakeUp for us
reactor.callFromThread(d.callback, None)
reactor.callInThread(wake)
return d
示例18
def save(self, reactor):
if not self.saving_lock:
self.saving_lock = True
reactor.callInThread(self._save)
示例19
def __listen_agent(self, agent):
reactor.callInThread(self._listen_agent, agent)
示例20
def call_in_thread(method, *args):
reactor.callInThread(method, *args)
示例21
def handle_store_messages(self):
for sender, messages in self.messages_buffer.items():
if self.agent_db_id.get(sender) == None:
r = ENGINE.execute(AGENTS.select(AGENTS.c.name.is_(sender)))
a = r.fetchall()
if a != []:
agent_id = a[0][0]
self.agent_db_id[sender] = agent_id
else:
print('Agent does not exist in database: {}'.format(sender))
r.close()
else:
pass
for message in messages:
receivers = ';'.join([i.localname for i in message.receivers])
content = message.content
if isinstance(content, ET.Element):
content = ET.tostring(content)
insert_obj = MESSAGES.insert()
sql_act = insert_obj.values(agent_id=self.agent_db_id[sender],
sender=message.sender.name,
date=message.datetime,
performative=message.performative,
protocol=message.protocol,
content=content,
conversation_id=message.conversation_id,
message_id=message.messageID,
ontology=message.ontology,
language=message.language,
receivers=receivers)
#reactor.callInThread(ENGINE.execute, sql_act)
reactor.callLater(random.uniform(0.1, 0.5), self.register_message_in_db, sql_act)
if self.debug:
display_message(self.aid.name, 'Message stored')
self.messages_buffer = dict()
self.buffer_control = True
示例22
def startService(self):
reactor.callInThread(writeForever)
Service.startService(self)
示例23
def callMultipleInThread(tupleList):
"""
Run a list of functions in the same thread.
tupleList should be a list of (function, argsList, kwargsDict) tuples.
"""
from twisted.internet import reactor
reactor.callInThread(_runMultiple, tupleList)
示例24
def testWakeUp(self):
# Make sure other threads can wake up the reactor
d = Deferred()
def wake():
time.sleep(0.1)
# callFromThread will call wakeUp for us
reactor.callFromThread(d.callback, None)
reactor.callInThread(wake)
return d
示例25
def callInThread(func, *args):
"""Takes a blocking function an converts it into a deferred-valued
function running in a separate thread.
"""
de = defer.Deferred()
de.addCallback(func)
reactor.callInThread(de.callback, *args)
return de
示例26
def _process(self):
try:
while self.consumers != [] and self.queue != []:
d = self.consumers.pop(0)
obj = self.queue.pop(0)
dt = threads.deferToThread(self._process_in_thread, d, obj)
#reactor.callInThread(self._process_in_thread, d, obj)
except Exception, e:
print str(e)
示例27
def main():
global plc, motor, nozzle, level, contact
# Initialise simulator
reactor.callInThread(runWorld)
# Initialise motor, nozzle, level and contact components
motor['server'] = Server(MOTOR_SERVER_IP, port=MOTOR_SERVER_PORT)
reactor.listenTCP(MOTOR_SERVER_PORT, motor['server'], interface = MOTOR_SERVER_IP,)
nozzle['server'] = Server(NOZZLE_SERVER_IP, port=NOZZLE_SERVER_PORT)
reactor.listenTCP(NOZZLE_SERVER_PORT, nozzle['server'], interface = NOZZLE_SERVER_IP,)
level['server'] = Server(LEVEL_SERVER_IP, port=LEVEL_SERVER_PORT)
reactor.listenTCP(LEVEL_SERVER_PORT, level['server'], interface = LEVEL_SERVER_IP,)
contact['server'] = Server(CONTACT_SERVER_IP, port=CONTACT_SERVER_PORT)
reactor.listenTCP(CONTACT_SERVER_PORT, contact['server'], interface = CONTACT_SERVER_IP)
# Initialise plc component
plc['server'] = Server(PLC_SERVER_IP, port=PLC_SERVER_PORT)
reactor.listenTCP(PLC_SERVER_PORT, plc['server'], interface = PLC_SERVER_IP)
plc['motor'] = Client(MOTOR_SERVER_IP, port=MOTOR_SERVER_PORT)
plc['nozzle'] = Client(NOZZLE_SERVER_IP, port=NOZZLE_SERVER_PORT)
plc['level'] = Client(LEVEL_SERVER_IP, port=LEVEL_SERVER_PORT)
plc['contact'] = Client(CONTACT_SERVER_IP, port=CONTACT_SERVER_PORT)
# Run World
reactor.run()
示例28
def __init__(self):
self._monitor_tx = {}
self._monitor_lock = threading.Lock()
self.last_status = time.time()
self.radar_hosts = 0
self.issues = 0
reactor.callInThread(self.status_loop)
reactor.callInThread(self.feedback_loop)
reactor.callLater(1, self.watchdog)
示例29
def __init__(self):
self.connector = BroadcastConnector()
self.last_status = time.time()
self.last_nodes = 0
self.issues = 0
self.notifications = defaultdict(list)
reactor.callInThread(self.status_loop)
reactor.callInThread(self.feedback_loop)
reactor.callLater(1, self.watchdog)
示例30
def log(sess_id, host, user, data):
reactor.callInThread(Logger.background_log, sess_id, host, user, data)