本文整理汇总了Python中twisted.python.threadable.isInIOThread函数的典型用法代码示例。如果您正苦于以下问题:Python isInIOThread函数的具体用法?Python isInIOThread怎么用?Python isInIOThread使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isInIOThread函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testIsInIOThread
def testIsInIOThread(self):
foreignResult = []
t = threading.Thread(target=lambda: foreignResult.append(threadable.isInIOThread()))
t.start()
t.join()
self.failIf(foreignResult[0], "Non-IO thread reported as IO thread")
self.failUnless(threadable.isInIOThread(), "IO thread reported as not IO thread")
示例2: test_isInIOThread
def test_isInIOThread(self):
"""
L{threadable.isInIOThread} returns C{True} if and only if it is called
in the same thread as L{threadable.registerAsIOThread}.
"""
threadable.registerAsIOThread()
foreignResult = []
t = threading.Thread(
target=lambda: foreignResult.append(threadable.isInIOThread()))
t.start()
t.join()
self.assertFalse(
foreignResult[0], "Non-IO thread reported as IO thread")
self.assertTrue(
threadable.isInIOThread(), "IO thread reported as not IO thread")
示例3: commit_now
def commit_now(self, vacuum=False, exiting=False):
if self._should_commit and isInIOThread():
try:
self._logger.info(u"Start committing...")
self.execute(u"COMMIT;")
except:
self._logger.exception(u"COMMIT FAILED")
raise
self._should_commit = False
if vacuum:
self._logger.info(u"Start vacuuming...")
self.execute(u"VACUUM;")
if not exiting:
try:
self._logger.info(u"Beginning another transaction...")
self.execute(u"BEGIN;")
except:
self._logger.exception(u"Failed to execute BEGIN")
raise
else:
self._logger.info(u"Exiting, not beginning another transaction")
elif vacuum:
self.execute(u"VACUUM;")
示例4: sendMessage
def sendMessage(self, sendType, dataDict, callback=None):
self.updateComplete = False
if callback != None:
self.factory.dataQueue.append(EmulatorResponse(dataDict, callback, self.factory))
self.factory.sendDataToServer(sendType, dataDict)
if not threadable.isInIOThread() and callback != None:
return self.waitForUpdate()
示例5: in_thread_wrapper
def in_thread_wrapper(*args, **kw):
if isInIOThread():
return func(*args, **kw)
f = Future()
def twisted_wrapper():
try:
d = func(*args, **kw)
if isinstance(d, Deferred):
def _done(result):
f.set_result(result)
f.done()
def _error(e):
f.set_exception(e)
f.done()
d.addCallback(_done)
d.addErrback(_error)
else:
f.set_result(d)
f.done()
except Exception, e:
f.set_exception(e)
f.done()
示例6: start
def start(self):
if self._started:
raise RuntimeError("worker has already been started")
if not threadable.isInIOThread():
raise RuntimeError("worker can only be started in the IO thread")
self._started = True
callInGreenThread(self.__run__)
示例7: shutdown
def shutdown(self):
"""
Checkpoints the session and closes it, stopping the download engine.
This method has to be called from the reactor thread.
"""
assert isInIOThread()
@inlineCallbacks
def on_early_shutdown_complete(_):
"""
Callback that gets called when the early shutdown has been completed.
Continues the shutdown procedure that is dependant on the early shutdown.
:param _: ignored parameter of the Deferred
"""
self.config.write()
yield self.checkpoint_downloads()
self.lm.shutdown_downloads()
self.lm.network_shutdown()
if self.lm.mds:
self.lm.mds.shutdown()
if self.sqlite_db:
self.sqlite_db.close()
self.sqlite_db = None
return self.lm.early_shutdown().addCallback(on_early_shutdown_complete)
示例8: get_id
def get_id(self, model, unique, fields):
''' Get an ID from the cache or from the database.
If doesn't exist - create an item.
All database operations are done from
the separate thread
'''
assert isInIOThread()
fval = fields[unique]
try:
result = self.cache[model][fval]
self.counters['hit'][model] += 1
returnValue(result)
except KeyError:
self.counters['miss'][model] += 1
selectors = {unique: fval}
result, created = yield deferToThreadPool(
self.reactor, self.read_pool,
get_or_create,
model, fields, **selectors)
result = result.id
if created:
self.counters['db_create'][model] += 1
else:
self.counters['db_hit'][model] += 1
self.cache[model][fval] = result
returnValue(result)
示例9: call_in_twisted_thread
def call_in_twisted_thread(func, *args, **kwargs):
if threadable.isInIOThread():
func(*args, **kwargs)
else:
from twisted.internet import reactor
reactor.callFromThread(func, *args, **kwargs)
示例10: _do_save
def _do_save(self):
assert not isInIOThread()
while not self.write_queue.empty():
items = []
try:
self.writelock = True
try:
while True:
items.append(self.write_queue.get_nowait())
except Empty:
pass
session = Session()
try:
session.add_all(items)
session.commit()
except:
session.rollback()
raise
finally:
session.close()
finally:
self.writelock = False
示例11: __call__
def __call__(self, environ, start_response):
"""
This function have to be called in a worker thread, not the IO thread.
"""
rargs = environ['wsgiorg.routing_args'][1]
controller = rargs['controller']
# Media Transport
if controller == 'mt':
name = rargs['name']
if name in self.mts:
return self.mts[name](environ, start_response)
else:
return not_found(environ, start_response)
if controller != 'upnp':
return not_found(environ, start_response)
try:
udn = rargs['udn']
if isInIOThread():
# TODO: read request body
return self.devices[udn](environ, start_response)
else:
# read request body
input = environ['wsgi.input']
environ['upnp.body'] = input.read(self.SOAP_BODY_MAX)
# call the app in IO thread
args = [udn, environ, start_response]
blockingCallFromThread(self.reactor, self._call_handler, args)
return args[3]
except Exception, e:
#print e
#print 'Unknown access: ' + environ['PATH_INFO']
return not_found(environ, start_response)
示例12: wait
def wait(self, timeout=None):
"""
Return the result, or throw the exception if result is a failure.
It may take an unknown amount of time to return the result, so a
timeout option is provided. If the given number of seconds pass with
no result, a TimeoutError will be thrown.
If a previous call timed out, additional calls to this function will
still wait for a result and return it if available. If a result was
returned or raised on one call, additional calls will return/raise the
same result.
"""
if threadable.isInIOThread():
raise RuntimeError("EventualResult.wait() must not be run in the reactor thread.")
if imp.lock_held():
# If EventualResult.wait() is run during module import, if the
# Twisted code that is being run also imports something the result
# will be a deadlock. Even if that is not an issue it would
# prevent importing in other threads until the call returns.
raise RuntimeError("EventualResult.wait() must not be run at module import time.")
result = self._result(timeout)
if isinstance(result, Failure):
result.raiseException()
return result
示例13: __init__
def __init__(self, crawler, update_vars=None, code=None):
self.crawler = crawler
self.update_vars = update_vars or (lambda x: None)
self.item_class = load_object(crawler.settings['DEFAULT_ITEM_CLASS'])
self.spider = None
self.inthread = not threadable.isInIOThread()
self.code = code
self.vars = {}
示例14: _queue_action
def _queue_action(self, action, event=None):
if isinstance(action, Action):
if isInIOThread():
self.bot.route_response(action, event)
else:
reactor.callFromThread(self.bot.route_response, action, event)
else:
self.log.error('tried to queue invalid action: {0!r}'.format(action))
示例15: write
def write(self, *args):
"""Ensure that all writes are serialized regardless if the command is executing in another thread.
"""
if self.write_buffer is not None and hasattr(self.write_buffer, 'append'):
self.write_buffer.append(' '.join(map(str, args)))
deferredMethod = (reactor.callFromThread if not isInIOThread() else defer.maybeDeferred)
# XXX: HACK: force str for the first param to avoid UnicodeDecodeError happening in Conch
return deferredMethod(self.terminal.write, str(args[0]), *args[1:])