Here are the examples of the python api twisted.internet.threads.blockingCallFromThread taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
37 Examples
3
Example 1
View licensedef fetch(self, request_or_url, spider=None): if isinstance(request_or_url, Request): request = request_or_url url = request.url else: url = any_to_uri(request_or_url) request = Request(url, dont_filter=True) request.meta['handle_httpstatus_all'] = True response = None try: response, spider = threads.blockingCallFromThread( reactor, self._schedule, request, spider) except IgnoreRequest: pass self.populate_vars(response, request, spider)
3
Example 2
View licensedef _eval(deferred, reactor): """Evaluate a deferred on a given reactor and return the result This function is safe to call with a deferred that has already been evaluated. """ @defer.inlineCallbacks def closure(): if deferred.called: result = deferred.result else: result = yield deferred defer.returnValue(result) if threading.currentThread().getName() == reactor.thread_name: return closure() else: return threads.blockingCallFromThread(reactor, closure)
3
Example 3
View license@_catch_remote_errors def connect(self): """Connect to wpa_supplicant over D-Bus :returns: Remote D-Bus proxy object of the root wpa_supplicant interface :rtype: :class:`~WpaSupplicant` """ if not self._reactor.running: raise ReactorNotRunning('Twisted Reactor must be started (call .run())') @defer.inlineCallbacks def get_conn(): self._reactor.thread_name = threading.currentThread().getName() conn = yield client.connect(self._reactor, busAddress='system') defer.returnValue(conn) conn = threads.blockingCallFromThread(self._reactor, get_conn) return WpaSupplicant('/fi/w1/wpa_supplicant1', conn, self._reactor, )
3
Example 4
View licensedef start(self, port=0, boot_strap=[]): self.kserver = self.server_type() self.kserver.bootstrap(boot_strap) self.port = threads.blockingCallFromThread(reactor, reactor.listenUDP, port, self.kserver.protocol) print "Starting server:", self.port time.sleep(.2) return self.port.getHost().port, self.kserver
3
Example 5
View licensedef start(self, port=0, boot_strap=[]): self.kserver = self.server_type() self.kserver.bootstrap(boot_strap) self.port = threads.blockingCallFromThread(reactor, reactor.listenUDP, port, self.kserver.protocol) print "Starting server:", self.port time.sleep(.2) return self.port.getHost().port, self.kserver
3
Example 6
View licensedef start(self): self.client_factory = SpamClientFactory(self) self.connector = threads.blockingCallFromThread( self.reactor, self.reactor.connectTCP, self.host, self.port, self.client_factory, timeout=5.0 )
3
Example 7
View licensedef get_remote_metadata(self, dircap, basedir=''): metadata = {} jobs = [] logging.debug("Getting remote metadata from %s...", dircap) url = '{}uri/{}/?t=json'.format(self.tahoe.node_url, dircap) received_data = requests.get(url).json() for filename, data in received_data[1]['children'].items(): path = '/'.join([basedir, filename]).strip('/') metadata[path] = { 'uri': data[1]['ro_uri'], 'mtime': int(data[1]['metadata']['mtime'])} if data[0] == 'dirnode': jobs.append( deferToThread(self.get_remote_metadata, '/'.join([dircap, filename]), path)) results = blockingCallFromThread(reactor, gatherResults, jobs) for result in results: metadata.update(result) return metadata
3
Example 8
View license@classmethod def getNewConnectionFromThread(cls): """ Block the caller's thread, until a new pty has been received. It will ask the current shell to open a new terminal, and wait for a new socket connection which will initialize the new pseudo terminal. """ return threads.blockingCallFromThread(reactor, cls.getNewConnection)
3
Example 9
View license@classmethod def getNewConnectionFromThread(cls): """ Block the caller's thread, until a new pty has been received. It will ask the current shell to open a new terminal, and wait for a new socket connection which will initialize the new pseudo terminal. """ return threads.blockingCallFromThread(reactor, cls.getNewConnection)
3
Example 10
View licensedef build(self, path=None, fobj=None, tag=None, quiet=False, nocache=False, rm=False): """Run build of a container from buildfile that can be passed as local/remote path or file object(fobj) """ dockerfile = DockerFile(path, fobj) def call(): deferreds = [] for host in self.hosts: deferreds.append( self.c.build( host, dockerfile, tag=tag, quiet=quiet, nocache=nocache, rm=rm)) return defer.gatherResults(deferreds, consumeErrors=True) responses = threads.blockingCallFromThread(reactor, call) return [Response(h, 200, r) for h, r in zip(self.hosts, responses)]
3
Example 11
View licensedef parallel(self, method, params): def call(): if isinstance(params, dict): # we assume that it's all the same call to all default hosts # with the same arguments deferreds = [method(h, **copy(params)) for h in self.hosts] elif isinstance(params, list): # we assume that it's a list of tuples (host, kwargs) # (useful in case if you have parallel calls to # different endpoints) deferreds = [] for host, kwargs in params: deferreds.append(method(host, **copy(kwargs))) return defer.gatherResults(deferreds, consumeErrors=True) return threads.blockingCallFromThread(reactor, call)
3
Example 12
View licensedef build(self, path=None, fobj=None, tag=None, quiet=False, nocache=False, rm=False): """Run build of a container from buildfile that can be passed as local/remote path or file object(fobj) """ dockerfile = DockerFile(path, fobj) def call(): deferreds = [] for host in self.hosts: deferreds.append( self.c.build( host, dockerfile, tag=tag, quiet=quiet, nocache=nocache, rm=rm)) return defer.gatherResults(deferreds, consumeErrors=True) responses = threads.blockingCallFromThread(reactor, call) return [Response(h, 200, r) for h, r in zip(self.hosts, responses)]
3
Example 13
View licensedef parallel(self, method, params): def call(): if isinstance(params, dict): # we assume that it's all the same call to all default hosts # with the same arguments deferreds = [method(h, **copy(params)) for h in self.hosts] elif isinstance(params, list): # we assume that it's a list of tuples (host, kwargs) # (useful in case if you have parallel calls to # different endpoints) deferreds = [] for host, kwargs in params: deferreds.append(method(host, **copy(kwargs))) return defer.gatherResults(deferreds, consumeErrors=True) return threads.blockingCallFromThread(reactor, call)
3
Example 14
View licensedef _execute(self): """ Callback fired when the associated event is set. Run the C{action} callback on the wrapped descriptor in the main reactor thread and raise or return whatever it raises or returns to cause this event handler to be removed from C{self._reactor} if appropriate. """ return blockingCallFromThread( self._reactor, lambda: getattr(self._fd, self._action)())
3
Example 15
View licensedef _execute(self): """ Callback fired when the associated event is set. Run the C{action} callback on the wrapped descriptor in the main reactor thread and raise or return whatever it raises or returns to cause this event handler to be removed from C{self._reactor} if appropriate. """ return blockingCallFromThread( self._reactor, lambda: getattr(self._fd, self._action)())
0
Example 16
View licensedef _process_file_in_thread(self, in_file, out_file): for line in in_file: line = line.strip() self.emit(out_file, line) threads.blockingCallFromThread(reactor, self.process_line, line)
0
Example 17
View licensedef __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)
0
Example 18
View licensedef _list_blocking(self): return blockingCallFromThread(self._reactor, self._list)
0
Example 19
View licensedef transaction(interaction): """ A decorator to wrap any code in a transaction. If any exceptions are raised, all modifications are rolled back. The function that is decorated should accept at least one argument, which is the transaction (in case you want to operate directly on it). """ def _transaction(txn, args, kwargs): config = Registry.getConfig() config.txn = txn # get the result of the functions *synchronously*, since this is in a transaction try: result = threads.blockingCallFromThread(reactor, interaction, txn, *args, **kwargs) config.txn = None return result except Exception, e: config.txn = None raise TransactionError(str(e)) def wrapper(*args, **kwargs): return Registry.DBPOOL.runInteraction(_transaction, args, kwargs) return wrapper
0
Example 20
View licensedef __init__(self, obj, *args, **kwargs): self._obj = threads.blockingCallFromThread(reactor, obj, *args, **kwargs)
0
Example 21
View licensedef _call(self, func, *args, **kwargs): return threads.blockingCallFromThread(reactor, func, *args, **kwargs)
0
Example 22
View licensedef main(iface): ret = 0 try: a = AutoDHTServer() a.start(iface) b = AutoDHTServer() b.start(iface) time.sleep(4) print a.set(key="APA", value="banan") print a.get(key="APA") print b.get(key="APA") a.stop() b.stop() except: traceback.print_exc() ret = 1 finally: if reactor.running: threads.blockingCallFromThread(reactor, reactor.stop) return ret
0
Example 23
View licensedef stop(self): result = threads.blockingCallFromThread(reactor, self.port.stopListening)
0
Example 24
View licensedef __init__(self, obj, *args, **kwargs): self._obj = threads.blockingCallFromThread(reactor, obj, *args, **kwargs)
0
Example 25
View licensedef _call(self, func, *args, **kwargs): return threads.blockingCallFromThread(reactor, func, *args, **kwargs)
0
Example 26
View licensedef main(iface): ret = 0 try: a = AutoDHTServer() a.start(iface) b = AutoDHTServer() b.start(iface) time.sleep(4) print a.set(key="APA", value="banan") print a.get(key="APA") print b.get(key="APA") a.stop() b.stop() except: traceback.print_exc() ret = 1 finally: if reactor.running: threads.blockingCallFromThread(reactor, reactor.stop) return ret
0
Example 27
View licensedef stop(self): result = threads.blockingCallFromThread(reactor, self.port.stopListening)
0
Example 28
View licensedef run(self, command, on_stdout=None, on_stderr=None, on_exit=None, line_buffered=True, kill_timeout=None, env=None): """Call this function to start a new child process running `command`. Additional callbacks, such as `on_stdout`, `on_stderr`, and `on_exit`, can be provided, that will receive a variety of parameters on the appropriate events. Line buffering can be disabled by passing `line_buffered`=False. Also, a custom `kill_timeout` (seconds) may be set that overrides the task default, in the event that a shutdown is received and you want to allow more time for the command to shut down.""" self.logger.debug("task starting %s...", command) if isinstance(command, six.string_types): command = command.split(" ") # wrap on_exit with helper to remove registered comments on_exit = functools.partial(self._procExited, on_exit) proto = _ProcessProtocolAdapter(on_stdout, on_stderr, on_exit, line_buffered=line_buffered) if twisted.python.threadable.isInIOThread(): result = self.reactor.spawnProcess(proto, executable=command[0], args=command) else: result = twisted.internet.threads.blockingCallFromThread( self.reactor, self.reactor.spawnProcess, proto, executable=command[0], args=command, env=env) self.outstanding[result] = kill_timeout self.started.increment() return result
0
Example 29
View licensedef spool_emails(self): mails = get_mails_from_the_pool() for mail in mails: threads.blockingCallFromThread(reactor, self.sendmail, mail)
0
Example 30
View licensedef sync(self, snapshot=None, force_backup=False): # flake8: noqa # FIXME ... if self.sync_state: logging.debug("Sync already in progress; queueing to end...") self.do_sync = True return if not snapshot: try: ls = self.tahoe.ls(self.remote_dircap) if not ls: logging.debug("No /Archives found; " "performing (first?) backup...") self.sync_state += 1 self.backup(self.local_dir, self.remote_dircap_alias) self.sync_complete(ls) return except Exception as error: logging.error(error) return # XXX: It might be preferable to just check the dircap of /Latest/ pre_sync_archives = self.tahoe.ls(self.remote_dircap + "/Archives") available_snapshot = pre_sync_archives[-1] if self.local_snapshot == available_snapshot: if force_backup: self.sync_state += 1 self.backup(self.local_dir, self.remote_dircap_alias) self.sync_complete(pre_sync_archives) return else: snapshot = available_snapshot remote_path = self.remote_dircap + '/Archives/' + snapshot logging.info("Syncing %s with %s...", self.local_dir, snapshot) self.sync_state += 1 local_metadata = self.get_local_metadata(self.local_dir) remote_metadata = self.get_remote_metadata(remote_path) # TODO: If tahoe.get_metadata() fails or doesn't contain a # valid snapshot, jump to backup? jobs = [] for file, metadata in remote_metadata.items(): if metadata['uri'].startswith('URI:DIR'): dirpath = os.path.join(self.local_dir, file) if not os.path.isdir(dirpath): logging.info("Creating directory: %s...", dirpath) os.makedirs(dirpath) for file, metadata in remote_metadata.items(): if not metadata['uri'].startswith('URI:DIR'): filepath = os.path.join(self.local_dir, file) remote_mtime = metadata['mtime'] if filepath in local_metadata: #local_filesize = local_metadata[filepath]['size'] local_mtime = local_metadata[filepath]['mtime'] if local_mtime < remote_mtime: logging.debug( "[<] %s is older than remote version; " "downloading %s...", file, file) if self.keep_versions: self._create_versioned_copy(filepath, local_mtime) jobs.append( deferToThread( self.download, remote_path + '/' + file, filepath, remote_mtime)) elif local_mtime > remote_mtime: logging.debug( "[>] %s is newer than remote version; " "backup scheduled", file) self.do_backup = True else: logging.debug("[.] %s is up to date.", file) else: logging.debug( "[?] %s is missing; downloading %s...", file, file) jobs.append( deferToThread( self.download, remote_path + '/' + file, filepath, remote_mtime)) for file, metadata in local_metadata.items(): fn = file.split(self.local_dir + os.path.sep)[1] if fn not in remote_metadata: if metadata: recovery_uri = self.tahoe.stored( file, metadata['size'], metadata['mtime']) if recovery_uri: logging.debug( "[x] %s removed from latest snapshot; " "deleting local file...", file) if self.keep_versions: self._create_versioned_copy(file, local_mtime) try: os.remove(file) except Exception as error: logging.error(error) else: logging.debug( "[!] %s isn't stored; backup scheduled", fn) self.do_backup = True blockingCallFromThread(reactor, gatherResults, jobs) if self.do_backup: self.backup(self.local_dir, self.remote_dircap_alias) self.do_backup = False if self.do_sync: self.sync() self.sync_complete(pre_sync_archives)
0
Example 31
View licensedef _testBlockingCallFromThread(self, reactorFunc): """ Utility method to test L{threads.blockingCallFromThread}. """ waiter = threading.Event() results = [] errors = [] def cb1(ign): def threadedFunc(): try: r = threads.blockingCallFromThread(reactor, reactorFunc) except Exception, e: errors.append(e) else: results.append(r) waiter.set() reactor.callInThread(threadedFunc) return threads.deferToThread(waiter.wait, self.getTimeout()) def cb2(ign): if not waiter.isSet(): self.fail("Timed out waiting for event") return results, errors return self._waitForThread().addCallback(cb1).addBoth(cb2)
0
Example 32
View licensedef _testBlockingCallFromThread(self, reactorFunc): """ Utility method to test L{threads.blockingCallFromThread}. """ waiter = threading.Event() results = [] errors = [] def cb1(ign): def threadedFunc(): try: r = threads.blockingCallFromThread(reactor, reactorFunc) except Exception, e: errors.append(e) else: results.append(r) waiter.set() reactor.callInThread(threadedFunc) return threads.deferToThread(waiter.wait, self.getTimeout()) def cb2(ign): if not waiter.isSet(): self.fail("Timed out waiting for event") return results, errors return self._waitForThread().addCallback(cb1).addBoth(cb2)
0
Example 33
View license@classmethod def shutdown(cls): """Shuts down connection pool""" threads.blockingCallFromThread( reactor, cls.pool.closeCachedConnections)
0
Example 34
View license@classmethod def shutdown(cls): """Shuts down connection pool""" threads.blockingCallFromThread( reactor, cls.pool.closeCachedConnections)
0
Example 35
View licensedef _testBlockingCallFromThread(self, reactorFunc): """ Utility method to test L{threads.blockingCallFromThread}. """ waiter = threading.Event() results = [] errors = [] def cb1(ign): def threadedFunc(): try: r = threads.blockingCallFromThread(reactor, reactorFunc) except Exception, e: errors.append(e) else: results.append(r) waiter.set() reactor.callInThread(threadedFunc) return threads.deferToThread(waiter.wait, self.getTimeout()) def cb2(ign): if not waiter.isSet(): self.fail("Timed out waiting for event") return results, errors return self._waitForThread().addCallback(cb1).addBoth(cb2)
0
Example 36
View licensedef _testBlockingCallFromThread(self, reactorFunc): """ Utility method to test L{threads.blockingCallFromThread}. """ waiter = threading.Event() results = [] errors = [] def cb1(ign): def threadedFunc(): try: r = threads.blockingCallFromThread(reactor, reactorFunc) except Exception, e: errors.append(e) else: results.append(r) waiter.set() reactor.callInThread(threadedFunc) return threads.deferToThread(waiter.wait, self.getTimeout()) def cb2(ign): if not waiter.isSet(): self.fail("Timed out waiting for event") return results, errors return self._waitForThread().addCallback(cb1).addBoth(cb2)
0
Example 37
View licensedef synchronizedInThread(R=None): """This decorator will make a blocking call from a thread to the reactor and wait for all callbacks. the result will be from the callback chain. Effectively useless unless you are in a seperate thread. #only good use case for this is in the decorators below ``threadedSafeDeferred`` and ``threadedSafePoolDeferred`` ... use of this decorator is discouraged unless you know what it does. @param R (reactor) @raise any exception @return (object) """ if not R: try: import config reactor = config.reactor except: from twisted.internet import reactor R = reactor def decorator(func): def newfunc(*a, **kw): #works with deferred and blocking methods return threads.blockingCallFromThread(R, func, *a, **kw) return newfunc return decorator