示例#1
0
async def rollback(session, players):
    # async rollback and count score changes
    loop = asyncio.get_event_loop()
    with QThreadExecutor(1) as executor: 
        await loop.run_in_executor(executor, session.rollback)
        changes = await loop.run_in_executor(executor, players.count_diff)
        return changes
示例#2
0
文件: patch.py 项目: xll98/FeelUOwn
def run_in_executor(self, executor, callback, *args):
    """Run callback in executor.

    If no executor is provided, the default executor will be used, which defers execution to
    a background thread.
    """
    # pylint: disable=all

    self._logger.debug('Running callback {} with args {} in executor'.format(callback, args))
    if isinstance(callback, asyncio.Handle):
        assert not args
        assert not isinstance(callback, asyncio.TimerHandle)
        if callback._cancelled:
            f = asyncio.Future()
            f.set_result(None)
            return f
        callback, args = callback.callback, callback.args

    if executor is None:
        self._logger.debug('Using default executor')
        executor = self.get_default_executor()

    if executor is None:
        self._logger.debug('Creating default executor')
        self.set_default_executor_v2(QThreadExecutor())
        executor = self.get_default_executor()

    return asyncio.wrap_future(executor.submit(callback, *args), loop=self)
示例#3
0
 async def run_exe(self):
     while True:
         try:
             with QThreadExecutor(1) as exec:
                 line = await self.loop.run_in_executor(exec, input, ">>> ")
             evl = eval(line)
             if isawaitable(evl):
                 r = await evl
                 print(r)
             else:
                 print(evl)
         except Exception as e:
             print(e)
示例#4
0
async def get_stats(wnd):
    loop = asyncio.get_event_loop()
    futures = {}
    updates = {}
    with QThreadExecutor(35) as executor:
        # prepare future objects and associate it with players db objects
        for player in wnd.players:
            url = "https://survarium.pro/api/v2/players/%s/stats" % player.name
            params = {'limit': 50, 'skip': 0}
            future = loop.run_in_executor(executor, send_request, url, params)
            futures[future] = (player, url, params)

        # iter by future objects and read responses
        for future in futures.keys():
            # unpack tupe associated with current future
            player, url, params = futures[future]
            scores_to_add = 0
            response = await future
            # if the player is new set last match id
            if player.match_id == 0:
                player.match_id = response['data'][0]['match']['id']
            else:
                # remember las match id and go to add scores
                last_match_id = response['data'][0]['match']['id']
                is_next = True
                while is_next:
                    for match in response['data']:
                        if player.match_id == match['match']['id']:
                            # if we found last match on this page break from loops
                            is_next = False
                            break
                        scores_to_add += match['score'] * 0.01
                    else:
                        # if last match doesn't on this page, get the next one
                        params['skip'] += 50
                        response = await loop.run_in_executor(executor, send_request, url, params)
                # set new scores and rank
                player.match_id = last_match_id
                player.scores += scores_to_add
                if scores_to_add != 0:                    
                    updates[player.name] = scores_to_add
                if player.scores < 0:
                    player.scores = 0
                if  player.scores > wnd.ranks[-1].scores:
                    player.scores = wnd.ranks[-1].scores
                # find rank by scores
                player.rank = wnd.find_rank(player.scores)
                # update progress bar
                wnd.progressBar.setValue(wnd.progressBar.value() + (95 / len(futures)))
    wnd.progressBar.setValue(100)
    return updates
示例#5
0
 async def game_status_check(self):
     logging.info('Checking local installation...')
     self.launch_game_btn.setText(_('Checking local installation...'))
     self.launch_game_btn.setEnabled(False)
     start = time.time()
     with QThreadExecutor(get_thread_count()) as exec:
         await asyncio.gather(*[
             loop.run_in_executor(exec, lambda: branch.index_directory())
             for branch in self.branches.values()
         ])
     logging.info('Local installation check completed.')
     logging.info('Game status check took %s seconds.' %
                  (time.time() - start))
     self.client_state = ClientState.GAME_UPDATE_CHECK
示例#6
0
def main():
    #QCoreApplication.setAttribute(Qt.AA_UseDesktopOpenGL)
    #QCoreApplication.setAttribute(Qt.AA_UseOpenGLES)
    app = MainApplication(sys.argv)
    loop = QEventLoop(app)
    asyncio.set_event_loop(loop)
    app.threadExecutor = QThreadExecutor(10)
    if app.isClosing():
        return 0

    #app.setProxyStyle(ProxyStyle())
    #return app.exec_()

    with loop:
        # context manager calls .close() when loop completes, and releases all resources
        loop.run_forever()
示例#7
0
async def get_players(wnd):
    # send two request to get two pages of players
    loop = asyncio.get_event_loop()
    url = "https://survarium.pro/api/v2/clans/%s/players/" % clan
    to_do = [(url, {'limit': 50, 'skip': 0}), (url, {'limit': 50, 'skip': 50})]
    players = []
    futures = []
    # coroutine runned in QThreadExecutor doesn't block event loop
    with QThreadExecutor(2) as executor:
        for params in to_do:
            future = loop.run_in_executor(executor, send_request, *params)
            futures.append(future)
        for future in futures:
            response = await future
            players += response['data']
            wnd.progressBar.setValue(wnd.progressBar.value() + 2.5)
    return players
示例#8
0
def master():
    yield from first_50()
    with QThreadExecutor(1) as exec:
        yield from loop.run_in_executor(exec, last_50)
示例#9
0
async def master():
    await first_50()
    with QThreadExecutor(1) as exec:
        await loop.run_in_executor(exec, last_50)
示例#10
0
async def commit(session, players):
    # async commit to database and file dump
    loop = asyncio.get_event_loop()
    with QThreadExecutor(1) as executor:
        await loop.run_in_executor(executor, session.commit)
        await loop.run_in_executor(executor, players.save_dump)
示例#11
0
 def master(self):
     yield from self.first_50()
     with QThreadExecutor(1) as exec:
         yield from loop.run_in_executor(exec, self.last_50)
     print('ready!')
示例#12
0
async def threadExec(fn, *args):
    loop = asyncio.get_event_loop()

    with QThreadExecutor(1) as texec:
        return await loop.run_in_executor(texec, fn, *args)
示例#13
0
    async def fetch_remote_index(self, context, progress_bar):
        branch_context = dict(context)
        branch_context["branch"] = self.source_branch
        url = inject_variables(self.config.index_endpoint, branch_context)
        logging.info('Fetching remote index from %s...' % url)
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                self.remote_index = await response.json()
        file_downloads = {}
        branch_context['base_url'] = self.remote_index['base_url']
        url_format = self.remote_index['url_format']
        download_bytes = 0
        download_tracker = DownloadTracker(progress_bar)
        logging.info('Comparing local installation against remote index...')
        for filename, filedata in self.remote_index['files'].items():
            filehash = filedata['sha256']
            filesize = filedata['size']
            branch_context['filename'] = filename
            branch_context['filehash'] = filehash
            url = inject_variables(url_format, branch_context)

            file_path = os.path.join(self.directory, filename)
            download = None
            if filename not in self.files:
                download = Download(file_path, url, filesize)
                logging.info('Missing file: (%s) %s ' % (filehash, filename))
            elif self.files[filename] != filehash:
                download = Download(file_path, url, filesize)
                logging.info('Hash mismatch: %s (%s vs %s)' %
                             (filename, filehash, self.files[filename]))
            else:
                logging.info('Matched File: %s (%s)' % (filehash, filename))
            if download is not None:
                download_tracker.downloads.append(download)
        logging.info('Total download size: %s' % download_bytes)
        directories = {
            os.path.dirname(download.file_path)
            for download in download_tracker.downloads
        }
        for directory in directories:
            if not os.path.exists(directory):
                logging.info('Creating new directory: %s' % directory)
                os.makedirs(directory)
        with QThreadExecutor(get_thread_count()) as executor:
            async with aiohttp.ClientSession() as session:
                files = list()
                for download in download_tracker.downloads:
                    path = download.file_path
                    if os.path.isdir(path):
                        logging.info('Delete conflicting directory: %s' % path)
                        shutil.rmtree(path)
                    download_request = \
                        loop.run_in_executor(executor,
                                             download.download_file,
                                             session)
                    files.append(download_request)
                downloads = asyncio.gather(*files)
                while not downloads.done():
                    download_tracker.update()
                    await asyncio.sleep(0.1)
        files = filter(lambda f: f[1] not in self.remote_index['files'],
                       list_files(self.directory))
        for _, filename in files:
            logging.info('Extra file', filename)