coroutine

Synchronised threads coroutines

故事扮演 提交于 2019-11-29 12:10:55
I'm trying to get threads to wait for each other before preceding so they stay in sync. In my actual program I have lots of IObjectObserved objects (on their own threads) sending out events and I want to keep everything in sync so an IObjectListener (on its own thread) can listen to one of these objects 50 times and then subscribe to another in time to catch its 51st event. I haven't got that far yet, but I think synchronizing threads is the main problem. I'm managed to achieve this with two way signalling using AutoResetEvent s. Is there not a better way to do this? class Program { static

Curl 在 Swoole 协程中的解决方案

爷,独闯天下 提交于 2019-11-29 09:33:24
众所周知,在 Swoole 应用中,是不推荐使用 Curl 的,因为 Curl 会阻塞进程。 本文会用实际的代码和数据,用最直观的方式,让你明白为什么。 最后还会给出 Curl 在 Swoole 中的解决方案,如果不想看分析可以直接拉到最后。 例程对比 宇润看文章不喜欢那些虚的,所以自己写也比较实在,直接来跑一下代码,用数据看为什么不推荐在 Swoole 使用 Curl。 为了偷懒,我直接用了 YurunHttp 的 Curl 和 Swoole Handler,来替代那些又臭又长的 Curl 代码。 代码 composer.json { "require": { "yurunsoft/yurun-http": "~3.0" } } server.php <?php $http = new Swoole\Http\Server('127.0.0.1', 9501); $http->on('workerstart', function(){ \Swoole\Runtime::enableCoroutine(); }); $http->on('request', function ($request, $response) { sleep(1); // 假设各种处理耗时1秒 $response->end($request->get['id'] . ': ' . date('Y-m-d H

Test if function or method is normal or asynchronous

随声附和 提交于 2019-11-29 09:04:50
How can I find out if a function or method is a normal function or an async function? I would like my code to automatically support normal or async callbacks and need a way to test what type of function is passed. async def exampleAsyncCb(): pass def exampleNomralCb(): pass def isAsync(someFunc): #do cool dynamic python stuff on the function return True/False async def callCallback(cb, arg): if isAsync(cb): await cb(arg) else: cb(arg) And depending on what type of function gets passed it should either run it normally or with await. I tried various things but have no idea how to implement

Why doesn't asyncio always use executors?

ⅰ亾dé卋堺 提交于 2019-11-29 08:04:36
I have to send a lot of HTTP requests, once all of them have returned, the program can continue. Sounds like a perfect match for asyncio . A bit naively, I wrapped my calls to requests in an async function and gave them to asyncio . This doesn't work. After searching online, I found two solutions: use a library like aiohttp , which is made to work with asyncio wrap the blocking code in a call to run_in_executor To understand this better, I wrote a small benchmark. The server-side is a flask program that waits 0.1 seconds before answering a request. from flask import Flask import time app =

“ yield”关键字有什么作用?

牧云@^-^@ 提交于 2019-11-29 06:31:28
Python中 yield 关键字的用途是什么? 它有什么作用? 例如,我试图理解这段代码 1 : def _get_child_candidates(self, distance, min_dist, max_dist): if self._leftchild and distance - max_dist < self._median: yield self._leftchild if self._rightchild and distance + max_dist >= self._median: yield self._rightchild 这是呼叫者: result, candidates = [], [self] while candidates: node = candidates.pop() distance = node._get_dist(obj) if distance <= max_dist and distance >= min_dist: result.extend(node._values) candidates.extend(node._get_child_candidates(distance, min_dist, max_dist)) return result 调用 _get_child_candidates 方法时会发生什么? 是否返回列表?

Python 3.5: “async with” results in SyntaxError. Why? [duplicate]

时光怂恿深爱的人放手 提交于 2019-11-29 03:25:06
This question already has an answer here: How to use Asynchronous Comprehensions? 1 answer I am using Python 3.5, which, according to PEP 492 should have access to the async with syntax, yet I get a SyntaxError when I try to use it. What am I doing wrong? In [14]: sys.version Out[14]: '3.5.2 (default, Oct 11 2016, 04:59:56) \n[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)]' In [15]: async with aiohttp.ClientSession() as session: File "<ipython-input-15-9799c5ce74cf>", line 1 async with aiohttp.ClientSession() as session: ^ SyntaxError: invalid syntax You can not use async with without

Is coroutine a new thread in Unity3D?

夙愿已清 提交于 2019-11-29 03:01:05
I am confused and curious about how coroutines (in Unity3D and perhaps other places) work. Is coroutine a new thread? Unity's documentation they said: A coroutine is a function that can suspend its execution (yield) until the given YieldInstruction finishes. And they have C# examples here : using UnityEngine; using System.Collections; public class example : MonoBehaviour { void Start() { print("Starting " + Time.time); StartCoroutine(WaitAndPrint(2.0F)); print("Before WaitAndPrint Finishes " + Time.time); } IEnumerator WaitAndPrint(float waitTime) { yield return new WaitForSeconds(waitTime);

Python native coroutines and send()

让人想犯罪 __ 提交于 2019-11-29 01:27:34
Generator based coroutines have a send() method which allow bidirectional communication between the caller and the callee and resumes a yielded generator coroutine from the caller. This is the functionality that turns generators into coroutines. While the new native async/await coroutines provide superior support for async I/O, I do not see how to get the equivalent of send() with them. The use of yield in async functions is explicitly forbidden, so native coroutines can return only once using a return statement. Although await expressions bring new values into a coroutine, those values come

python - how to implement a C-function as awaitable (coroutine)

馋奶兔 提交于 2019-11-28 23:28:15
Environment: cooperative RTOS in C and micropython virtual machine is one of the tasks. To make the VM not block the other RTOS tasks, I insert RTOS_sleep() in vm.c:DISPATCH() so that after every bytecode is executed, the VM relinquishes control to the next RTOS task. I created a uPy interface to asynchronously obtain data from a physical data bus - could be CAN, SPI, ethernet - using producer-consumer design pattern. Usage in uPy: can_q = CANbus.queue() message = can_q.get() The implementation in C is such that can_q.get() does NOT block the RTOS: it polls a C-queue and if message is not

What are Lua coroutines even for? Why doesn't this code work as I expect it?

故事扮演 提交于 2019-11-28 18:45:57
问题 I'm having trouble understanding this code... I was expecting something similar to threading where I would get an output with random "nooo" and "yaaaay"s interspersed with each other as they both do the printing asynchronously, but rather I discovered that the main thread seems to block on the first calling of coroutine.resume() and thus prevents the next from being started until the first has yielded. If this is the intended operation coroutines, what are they useful for, and how would I