async def install() -> int: cols, _ = get_terminal_size() sep = cols * "=" errors: MutableSequence[str] = [] tasks = chain(_git(), _pip(), _gem(), _npm(), _go(), _script()) for fut in as_completed(tasks): for debug, proc in await fut: args = join(map(str, proc.args)) if proc.returncode == 0: msg = LANG("proc succeeded", args=args) print( msg, debug, decode(proc.stderr), decode(proc.stdout), sep, sep=linesep, ) else: errors.append(args) msg = LANG("proc failed", code=proc.returncode, args=args) print( msg, debug, decode(proc.stderr), decode(proc.stdout), sep, sep=linesep, file=stderr, ) if errors: print(linesep.join(errors)) return bool(errors)
def foo(): values = [] for f in tasks.as_completed([a, b], timeout=0.12, loop=loop): try: v = yield from f values.append((1, v)) except futures.TimeoutError as exc: values.append((2, exc)) return values
async def categorize_metrics( *futures: Awaitable[Iterable[Metric]], ) -> Tuple[Iterable[Metric], Iterable[Metric], Iterable[Metric]]: toplevel = [] objectlevel = [] subobjectlevel = [] for future in as_completed(futures): for metric in await future: if metric.object is None: toplevel.append(metric) elif metric.subobject is None: objectlevel.append(metric) else: subobjectlevel.append(metric) return toplevel, objectlevel, subobjectlevel
async def cont() -> AsyncIterator[Step]: for fut in as_completed(tuple(futures)): reply = await fut for suggestion in reply.suggestions: step = fuzzify(context, suggestion=suggestion, options=match_opt) yield step if reply.cache.enabled: await populate( conn, filetype=context.filetype, rank=reply.rank, suggestions=reply.suggestions, )
def test_as_completed_concurrent(self): def gen(): when = yield self.assertAlmostEqual(0.05, when) when = yield 0 self.assertAlmostEqual(0.05, when) yield 0.05 loop = test_utils.TestLoop(gen) self.addCleanup(loop.close) a = tasks.sleep(0.05, 'a', loop=loop) b = tasks.sleep(0.05, 'b', loop=loop) fs = {a, b} futs = list(tasks.as_completed(fs, loop=loop)) self.assertEqual(len(futs), 2) waiter = tasks.wait(futs, loop=loop) done, pending = loop.run_until_complete(waiter) self.assertEqual(set(f.result() for f in done), {'a', 'b'})
async def install() -> int: has_error = False for fut in as_completed(chain(_git(), _pip(), _npm(), _go(), _script())): for debug, proc in await fut: cols, _ = get_terminal_size((80, 40)) sep = cols * "=" args = " ".join(chain((proc.prog, ), proc.args)) if proc.code == 0: msg = LANG("proc succeeded", args=args) print(msg) print(debug) print(proc.out.decode()) print(sep) else: has_error = True msg = LANG("proc failed", code=proc.code, args=args) print(msg, file=stderr) print(debug, file=stderr) print(proc.err, file=stderr) print(sep, file=stderr) return has_error
def test_as_completed_reverse_wait(self): def gen(): yield 0 yield 0.05 yield 0 loop = test_utils.TestLoop(gen) self.addCleanup(loop.close) a = tasks.sleep(0.05, 'a', loop=loop) b = tasks.sleep(0.10, 'b', loop=loop) fs = {a, b} futs = list(tasks.as_completed(fs, loop=loop)) self.assertEqual(len(futs), 2) x = loop.run_until_complete(futs[1]) self.assertEqual(x, 'a') self.assertAlmostEqual(0.05, loop.time()) loop.advance_time(0.05) y = loop.run_until_complete(futs[0]) self.assertEqual(y, 'b') self.assertAlmostEqual(0.10, loop.time())
def foo(): values = [] for f in tasks.as_completed([b, c, a], loop=loop): values.append((yield from f)) return values
async def _slurp(nvim: Nvim, stack: Stack, warn: AbstractSet[SnippetWarnings]) -> None: with timeit("LOAD SNIPS"): ( cwd, bundled, (user_compiled, user_compiled_mtimes), (_, user_snips_mtimes), mtimes, ) = await gather( async_call(nvim, get_cwd, nvim), _bundled_mtimes(nvim), _load_user_compiled(stack.supervisor.vars_dir), user_mtimes(nvim, user_path=stack.settings.clients.snippets.user_path), stack.sdb.mtimes(), ) stale = mtimes.keys() - (bundled.keys() | user_compiled.keys()) compiled = { path: mtime for path, mtime in chain(bundled.items(), user_compiled.items()) if mtime > mtimes.get(path, -inf) } new_user_snips = { fmt_path(cwd, path=path, is_dir=False): ( datetime.fromtimestamp(mtime).strftime( stack.settings.display.time_fmt), datetime.fromtimestamp(prev).strftime( stack.settings.display.time_fmt) if (prev := user_compiled_mtimes.get(path)) else "??", ) for path, mtime in user_snips_mtimes.items() if mtime > user_compiled_mtimes.get(path, -inf) } await stack.sdb.clean(stale) if SnippetWarnings.missing in warn and not (bundled or user_compiled): await sleep(0) await awrite(nvim, LANG("fs snip load empty")) for fut in as_completed( tuple( _load_compiled(path, mtime) for path, mtime in compiled.items())): try: path, mtime, loaded = await fut except (OSError, JSONDecodeError, DecodeError) as e: tpl = """ Failed to load compiled snips ${e} """.rstrip() log.warn("%s", Template(dedent(tpl)).substitute(e=type(e))) else: await stack.sdb.populate(path, mtime=mtime, loaded=loaded) await awrite( nvim, LANG( "fs snip load succ", path=fmt_path(cwd, path=path, is_dir=False), ), ) if SnippetWarnings.outdated in warn and new_user_snips: paths = linesep.join(f"{path} -- {prev} -> {cur}" for path, (cur, prev) in new_user_snips.items()) await awrite(nvim, LANG("fs snip needs compile", paths=paths)) @rpc(blocking=True) def _load_snips(nvim: Nvim, stack: Stack) -> None: go(nvim, aw=_slurp(nvim, stack=stack, warn=stack.settings.clients.snippets.warn)) atomic.exec_lua(f"{NAMESPACE}.{_load_snips.name}()", ()) def compile_one( stack: Stack, grammar: SnippetGrammar, path: PurePath, info: ParseInfo, lines: Iterable[Tuple[int, str]], ) -> Compiled: filetype, exts, snips = load_neosnippet(grammar, path=path, lines=lines) parsed = tuple( _trans( stack.settings.match.unifying_chars, smart=stack.settings.completion.smart, info=info, snips=snips, )) compiled = Compiled( path=path, filetype=filetype, exts=exts, parsed=parsed, ) return compiled async def compile_user_snippets(nvim: Nvim, stack: Stack) -> None: with timeit("COMPILE SNIPS"): info = ParseInfo(visual="", clipboard="", comment_str=("", "")) _, mtimes = await user_mtimes( nvim, user_path=stack.settings.clients.snippets.user_path) loaded = await to_thread(lambda: load_direct( False, lsp=(), neosnippet=mtimes, ultisnip=(), neosnippet_grammar=SnippetGrammar.lsp, )) _ = tuple( _trans( stack.settings.match.unifying_chars, smart=stack.settings.completion.smart, info=info, snips=loaded.snippets.values(), )) try: await _dump_compiled(stack.supervisor.vars_dir, mtimes=mtimes, loaded=loaded) except OSError as e: await awrite(nvim, e) else: await _slurp(nvim, stack=stack, warn={SnippetWarnings.missing})