async

@asyncio.coroutine vs async def

匿名 (未验证) 提交于 2019-12-03 01:26:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: With the asyncio library I've seen, @asyncio.coroutine def function(): ... and async def function(): ... used interchangeably. Is there any functional difference between the two? 回答1: Yes, there are functional differences between native coroutines using async def syntax and generator-based coroutines using the asyncio.coroutine decorator. According to PEP 492 , which introduces the async def syntax: Native coroutine objects do not implement __iter__ and __next__ methods. Therefore, they cannot be iterated over or passed to iter() , list() ,

Console App Terminating Before async Call Completion

匿名 (未验证) 提交于 2019-12-03 01:25:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm currently writing a C# console app that generates a number of URLs that point to different images on a web site and then downloads as byte streams using WebClient.DownloadDataAsync() . My issue is that once the first asynchronous call is made, the console app considers the program to be completed and terminates before the asynchronous call can return. By using a Console.Read() I can force the console to stay open but this doesn't seem like very good design. Furthermore if the user hits enter during the process (while the console is

Async await vs GetAwaiter().GetResult() and callback

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to find the best practice for one of my project. It is a typical WPF application with a UI that displays a list of items and there is a data service that returns the result. We are calling the service asynchronously so as to not block the UI. We have 2 options in front of us: Using Async await keywords This requires marking all the methods Async from button click all the way to service layer (class on client side that makes the http call to the server) and any method in between. This approach works fine other then the issue of

Async error “Current thread must have a looper” when clicking retry button

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an app that reads some data from a website and then creates TextViews for what is retrieved from the website. I have the process working through an AsyncTask . I've got it set up so that if there is a network error while trying to read from the website, a Retry button is shown. My code works perfect when it runs through the first time, but when I try to run the code from the onClick of the button, I get the following error: java.lang.RuntimeException: An error occured while executing doInBackground() (a few lines of error code) Caused

How to set class attribute with await in __init__

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How can I define a class with await in the constructor or class body? For example what I want: import asyncio # some code class Foo(object): async def __init__(self, settings): self.settings = settings self.pool = await create_pool(dsn) foo = Foo(settings) # it raises: # TypeError: __init__() should return None, not 'coroutine' or example with class body attribute: class Foo(object): self.pool = await create_pool(dsn) # Sure it raises syntax Error def __init__(self, settings): self.settings = settings foo = Foo(settings) My solution (But I

C# Async Task Method Without Await or Return

匿名 (未验证) 提交于 2019-12-03 01:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have an Interface I that is implemented in two places like: interface I { Task DoSomething (); } The interface has async Task DoSomething method API that is then implemented in class A like: class A : I {....} class B : I {....} In class A, the implementation of DoSomething is like below and that is OK: public async Task DoSomething () { if (...) { await DoIt (); } } However, in class B, the implementation of DoSomething() should not do anything. So, its implementation looks like this: public async Task DoSomething () { //

C# async/await chaining with ConfigureAwait(false)

匿名 (未验证) 提交于 2019-12-03 01:19:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Based on numerous books and blogs including this excellent one here , it is clear that when one writes a dll library exposing helper async methods i.e. the wrapper methods, it is generally considered a best practice to internally complete the I/O task of actual async methods on a threadpool thread like so (pseudo code shown below for brevity and I'm using HttpClient as an example) public Async Task MyMethodAsync(..) { ... var httpClient = new HttpClient(..); var response = await httpClient.PostAsJsonAsync(..).ConfigureAwait(false); ...

How resume the execution of a stackful coroutine in the context of its strand?

匿名 (未验证) 提交于 2019-12-03 01:18:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: using Yield = asio::yield_context; using boost::system::error_code; int Func(Yield yield) { error_code ec; asio::detail::async_result_init init(yield[ec]); std::thread th(std::bind(Process, init.handler)); int result = init.result.get(); // How to implement Process so that Func will resumed in the context of the strand that Func was originally spawned on? 回答1: Boost.Asio uses a helper function, asio_handler_invoke , to provide a customization point for invocation strategies. For example, when a Handler has been wrapped by a strand , the

Promise.all error inside async function : undefined is not a function

匿名 (未验证) 提交于 2019-12-03 01:13:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: in my async function i use Promise.all but for some reason its not defined or something here is the function async function check_available_money() { global_browser = await puppeteer.launch({headless: false, args: ['--no-sandbox', '--disable-setuid-sandbox']}); const page = await global_browser.newPage(); await page.setViewport({width: 1000, height: 1100}); var setting = {'username': 'aa', 'password': 'bb'}; try { await page.goto('https://example.com/login', {timeout: 90000}) .catch(function (error) { throw new Error(' TIMEOUT 1 '); } );

How to write an “awaitable” method?

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm finally looking into the async & await keywords, which I kind of "get", but all the examples I've seen call async methods in the .Net framework, e.g. this one , which calls HttpClient.GetStringAsync() . What I'm not so clear on is what goes on in such a method, and how I would write my own "awaitable" method. Is it as simple as wrapping the code that I want to run asynchronously in a Task and returning that? 回答1: It's as simple as Task.Run(() => ExpensiveTask()); To make it an awaitable method: public Task ExpensiveTaskAsync() { return