Run  XenAPI task in asynchronous mode to prevent timeouts
963282
def _run_async_task(task=None, session=None):
    '''
    
    '''
    if task is None or session is None:
        return None
    task_name = session.xenapi.task.get_name_label(task)
    log.debug('Running %s', task_name)
    while session.xenapi.task.get_status(task) == 'pending':
        progress = round(session.xenapi.task.get_progress(task), 2) * 100
        log.debug('Task progress %.2f%%', progress)
        time.sleep(1)
    log.debug('Cleaning up task %s', task_name)
    session.xenapi.task.destroy(task)
406657
async def async_run_command(self, command, first_try=True):
        
        await self.async_connect()
        try:
            with (await self._io_lock):
                self._writer.write('{}\n'.format(
                    "%s && %s" % (
                        _PATH_EXPORT_COMMAND, command)).encode('ascii'))
                data = ((await asyncio.wait_for(self._reader.readuntil(
                    self._prompt_string), 9)).split(b'\n')[1:-1])

        except (BrokenPipeError, LimitOverrunError):
            if first_try:
                return await self.async_run_command(command, False)
            else:
                _LOGGER.warning("connection is lost to host.")
                return[]
        except TimeoutError:
            _LOGGER.error("Host timeout.")
            return []
        finally:
            self._writer.close()

        return [line.decode('utf-8') for line in data]
1297943
def wait(self, timeout=None):
    

    t_start = time.clock()
    if not wait_for_condition(self, lambda s: s._tasks == 0, timeout):
      raise Timeout
922718
def wait(self, timeout=None):
        
        try:
            if timeout:
                gevent.sleep(timeout)
            else:
                while True:
                    gevent.sleep(1000)
        except (KeyboardInterrupt, SystemExit, Exception):
            pass
728331
def xmlrpc_run(self, port=23333, bind='127.0.0.1', logRequests=False):
        ''''''
        from pyspider.libs.wsgi_xmlrpc import WSGIXMLRPCApplication

        application = WSGIXMLRPCApplication()

        application.register_function(self.quit, '_quit')
        application.register_function(self.__len__, 'size')

        def dump_counter(_time, _type):
            try:
                return self._cnt[_time].to_dict(_type)
            except:
                logger.exception('')
        application.register_function(dump_counter, 'counter')

        def new_task(task):
            if self.task_verify(task):
                self.newtask_queue.put(task)
                return True
            return False
        application.register_function(new_task, 'newtask')

        def send_task(task):
            '''dispatch task to fetcher'''
            self.send_task(task)
            return True
        application.register_function(send_task, 'send_task')

        def update_project():
            self._force_update_project = True
        application.register_function(update_project, 'update_project')

        def get_active_tasks(project=None, limit=100):
            allowed_keys = set((
                'type',
                'taskid',
                'project',
                'status',
                'url',
                'lastcrawltime',
                'updatetime',
                'track',
            ))
            track_allowed_keys = set((
                'ok',
                'time',
                'follows',
                'status_code',
            ))

            iters = [iter(x.active_tasks) for k, x in iteritems(self.projects)
                     if x and (k == project if project else True)]
            tasks = [next(x, None) for x in iters]
            result = []

            while len(result) < limit and tasks and not all(x is None for x in tasks):
                updatetime, task = t = max(t for t in tasks if t)
                i = tasks.index(t)
                tasks[i] = next(iters[i], None)
                for key in list(task):
                    if key == 'track':
                        for k in list(task[key].get('fetch', [])):
                            if k not in track_allowed_keys:
                                del task[key]['fetch'][k]
                        for k in list(task[key].get('process', [])):
                            if k not in track_allowed_keys:
                                del task[key]['process'][k]
                    if key in allowed_keys:
                        continue
                    del task[key]
                result.append(t)
            # fix for "<type 'exceptions.TypeError'>:dictionary key must be string"
            # have no idea why
            return json.loads(json.dumps(result))
        application.register_function(get_active_tasks, 'get_active_tasks')

        def get_projects_pause_status():
            result = {}
            for project_name, project in iteritems(self.projects):
                result[project_name] = project.paused
            return result
        application.register_function(get_projects_pause_status, 'get_projects_pause_status')

        def webui_update():
            return {
                'pause_status': get_projects_pause_status(),
                'counter': {
                    '5m_time': dump_counter('5m_time', 'avg'),
                    '5m': dump_counter('5m', 'sum'),
                    '1h': dump_counter('1h', 'sum'),
                    '1d': dump_counter('1d', 'sum'),
                    'all': dump_counter('all', 'sum'),
                },
            }
        application.register_function(webui_update, 'webui_update')

        import tornado.wsgi
        import tornado.ioloop
        import tornado.httpserver

        container = tornado.wsgi.WSGIContainer(application)
        self.xmlrpc_ioloop = tornado.ioloop.IOLoop()
        self.xmlrpc_server = tornado.httpserver.HTTPServer(container, io_loop=self.xmlrpc_ioloop)
        self.xmlrpc_server.listen(port=port, address=bind)
        logger.info('scheduler.xmlrpc listening on %s:%s', bind, port)
        self.xmlrpc_ioloop.start()
678804
async def work_async(self):
        """"""
        try:
            raise self._error
        except TypeError:
            pass
        except Exception as e:
            _logger.warning("%r", e)
            raise
        try:
            await self.lock_async()
            if self._closing:
                _logger.debug("Connection unlocked but shutting down.")
                return
            await self.loop.run_in_executor(self._executor, functools.partial(self._conn.do_work))
        except asyncio.TimeoutError:
            _logger.debug("Connection %r timed out while waiting for lock acquisition.", self.container_id)
        finally:
            self.release_async()
175511
def run_async(self, time_limit):
        '''  '''

        self.background = time_limit
        results = self.run()
        return results, poller.AsyncPoller(results, self)
876112
def send_rpc_async(self, conn_id, address, rpc_id, payload, timeout, callback):
        """"""

        future = self._loop.launch_coroutine(self._adapter.send_rpc(conn_id, address, rpc_id, payload, timeout))

        def format_response(future):
            payload = None
            exception = future.exception()
            rpc_status = None
            rpc_response = b''
            failure = None
            success = True

            if exception is None:
                payload = future.result()
                rpc_status, rpc_response = pack_rpc_response(payload, exception)
            elif isinstance(exception, (RPCInvalidIDError, TileNotFoundError, RPCNotFoundError,
                                        RPCErrorCode, BusyRPCResponse)):
                rpc_status, rpc_response = pack_rpc_response(payload, exception)
            else:
                success = False
                failure = str(exception)

            callback(conn_id, self.id, success, failure, rpc_status, rpc_response)

        future.add_done_callback(format_response)
892074
async def async_run_command(self, command, retry=False):
        
        if not self.is_connected:
            await self.async_connect()
        try:
            result = await asyncio.wait_for(self._client.run(
                "%s && %s" % (_PATH_EXPORT_COMMAND, command)), 9)
        except asyncssh.misc.ChannelOpenError:
            if not retry:
                await self.async_connect()
                return self.async_run_command(command, retry=True)
            else:
                self._connected = False
                _LOGGER.error("No connection to host")
                return []
        except TimeoutError:
            del self._client
            self._connected = False
            _LOGGER.error("Host timeout.")
            return []

        self._connected = True
        return result.stdout.split('\n')
915182
async def wait_and_quit(loop):
	""""""
	from pylp.lib.tasks import running
	if running:
		await asyncio.wait(map(lambda runner: runner.future, running))
379855
def run(delayed, concurrency, version_type=None, queue=None,
        raise_on_error=True):
    """"""
    if delayed:
        celery_kwargs = {
            'kwargs': {
                'version_type': version_type,
                'es_bulk_kwargs': {'raise_on_error': raise_on_error},
            }
        }
        click.secho(
            'Starting {0} tasks for indexing records...'.format(concurrency),
            fg='green')
        if queue is not None:
            celery_kwargs.update({'queue': queue})
        for c in range(0, concurrency):
            process_bulk_queue.apply_async(**celery_kwargs)
    else:
        click.secho('Indexing records...', fg='green')
        RecordIndexer(version_type=version_type).process_bulk_queue(
            es_bulk_kwargs={'raise_on_error': raise_on_error})
228996
def wait_for(self, timeout=3000):
        """"""
        results = [None]
        results_called = [False]

        def results_callback(val):
            results[0] = val
            results_called[0] = True

        self.then(results_callback)

        start = time.time()
        while not results_called[0]:
            if time.time() - start > timeout / 1000.:
                raise Exception('Timeout of %d ms reached' % timeout)
            ip.kernel.do_one_iteration()
        return results[0]
698486
private Object waitForResult(long timeoutMillis)
                    throws ExecutionException, InterruptedException, TimeoutException, RemoteException { // F16043
        final boolean isTraceOn = svLogger.isLoggable(Level.FINER);
        if (isTraceOn)
            svLogger.entering(CLASS_NAME, "waitForResult", Long.valueOf(timeoutMillis));

        long begin = System.nanoTime();
        // The nanoTime updated before every call to
        // RemoteAsyncResultExtended.waitForResult.
        long beginLastAttempt = begin;
        // The nanoTime when we should stop waiting altogether.  This value is
        // only used if timeout is non-zero.
        long end = begin + TimeUnit.MILLISECONDS.toNanos(timeoutMillis);

        // True if the client ORB has ever thrown a NO_RESPONSE.
        boolean noResponse = false;
        // The waitTime argument for the next call to
        // RemoteAsyncResultExtended.waitForResult.
        long waitTimeMillis = timeoutMillis;
        // The maximum value for waitTimeMillis.  This value is lazily
        // initialized when a NO_RESPONSE occurs.
        long maxWaitTimeMillis = -1;

        for (;;) {
            if (isTraceOn)
                svLogger.entering(CLASS_NAME, "waitForResult", "waiting " + waitTimeMillis);

            Object[] resultArray;
            try {
                resultArray = stubWaitForResult((Stub) ivServer, waitTimeMillis);
            } catch (RemoteException ex) {
                Throwable cause = ex.getCause();

                // If this is the first NO_RESPONSE, then try again with a shorter
                // server-side wait time.  For subsequent NO_RESPONSE, assume there
                // is a connection issue and rethrow the exception.
                if (cause instanceof NO_RESPONSE && !noResponse) {
                    if (isTraceOn)
                        svLogger.logp(Level.FINER, CLASS_NAME, "waitForResult", "retrying", cause);
                    resultArray = null;
                    noResponse = true;
                } else {
                    if (isTraceOn)
                        svLogger.exiting(CLASS_NAME, "waitForResult", ex);
                    throw ex;
                }
            }

            if (resultArray != null) {
                if (isTraceOn)
                    svLogger.exiting(CLASS_NAME, "waitForResult", "result");
                return resultArray[0];
            }

            // Either the client ORB or the server timed us out.  Choose a lower
            // waitTimeMillis and then try again.

            long now = System.nanoTime();

            if (timeoutMillis > 0) {
                long remainingMillis = TimeUnit.NANOSECONDS.toMillis(end - now);
                if (remainingMillis <= 0) {
                    if (isTraceOn)
                        svLogger.exiting(CLASS_NAME, "waitForResult", "timeout");
                    throw new TimeoutException();
                }

                waitTimeMillis = remainingMillis;
            }

            if (noResponse) {
                if (maxWaitTimeMillis == -1) {
                    // For the first NO_RESPONSE, set maximum server wait time for
                    // all subsequent requests based on how long the ORB actually
                    // waited before timing out this request.  Back off a little bit
                    // to give time for the server to respond.
                    long actualWaitTimeMillis = TimeUnit.NANOSECONDS.toMillis(now - beginLastAttempt);
                    maxWaitTimeMillis = Math.max(MIN_ASYNC_RESULT_NO_RESPONSE_WAIT_TIME,
                                                 actualWaitTimeMillis - AsyncResultNoResponseBackoff);
                }

                if (timeoutMillis == 0) {
                    waitTimeMillis = maxWaitTimeMillis;
                } else {
                    waitTimeMillis = Math.min(waitTimeMillis, maxWaitTimeMillis);
                }
            }

            beginLastAttempt = now;
        }
    }
1264956
def run_console(*, locals=None, banner=None, serve=None, prompt_control=None):
    """"""
    loop = InteractiveEventLoop(
        locals=locals,
        banner=banner,
        serve=serve,
        prompt_control=prompt_control)
    asyncio.set_event_loop(loop)
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
1083670
def run(self, timeout = 0):
        
        ptimeout = float(
            timeout.microseconds/1000000+timeout.seconds if timeout
            else (self.resolution if timeout is None else 0)
        )
        if self.tokens:
            events = self.epoll_obj.poll(ptimeout, 1024)
            len_events = len(events)-1
            for nr, (fd, ev) in enumerate(events):
                act = self.shadow.pop(fd)
                if ev & EPOLLHUP:
                    self.epoll_obj.unregister(fd)
                    self.handle_error_event(act, 'Hang up.', ConnectionClosed)
                elif ev & EPOLLERR:
                    self.epoll_obj.unregister(fd)
                    self.handle_error_event(act, 'Unknown error.')
                else:
                    if nr == len_events:
                        ret = self.yield_event(act)
                        if not ret:
                            self.epoll_obj.modify(fd, ev | EPOLLONESHOT)
                            self.shadow[fd] = act
                        return ret
                    else:
                        if not self.handle_event(act):
                            self.epoll_obj.modify(fd, ev | EPOLLONESHOT)
                            self.shadow[fd] = act


        else:
            sleep(timeout)
1293641
def _wait_for_async(conn, request_id):
    '''
    
    '''
    count = 0
    log.debug('Waiting for asynchronous operation to complete')
    result = conn.get_operation_status(request_id)
    while result.status == 'InProgress':
        count = count + 1
        if count > 120:
            raise ValueError('Timed out waiting for asynchronous operation to complete.')
        time.sleep(5)
        result = conn.get_operation_status(request_id)

    if result.status != 'Succeeded':
        raise AzureException('Operation failed. {message} ({code})'
                                .format(message=result.error.message,
                                        code=result.error.code))
158695
def _run_async(self, urls):
        
        loop = asyncio.get_event_loop()
        results = loop.run_until_complete(self._async_loop(urls))
        return results