coroutine

转:进程 线程 协程 管程 纤程 概念对比理解

孤街醉人 提交于 2020-03-20 23:49:02
3 月,跳不动了?>>> 引言 不知道是不是我自己本身就有那么一丝丝的密集恐惧,把这么一大堆看起来很相似很相关的概念放在一起,看起来是有点麻,捋一捋感觉舒服多了。 相关概念 任务、作业(Job,Task,Schedule) 在进程的概念出现之前,进程有着这样的称谓。 进程 为了使多个程序能够并发(同一时刻只有一个在运行,但感觉起来像多个同时运行;并行(同一时刻真的多个在运行,不是感觉像多个))的执行,操作系统需要一个结构来抽象和表示这个程序的运行。 进程是操作系统对一个正在运行的程序的一种抽象结构。 进程是指在操作系统中能独立运行并作为资源分配的基本单位,由一组机器指令、数据和堆栈等组成的能独立运行的活动实体。 操作系统可以同时运行多个进程,多个进程直接可以并发执行和交换信息。 进程在运行是需要一定的资源,如CPU、存储空间和I/O设备等。 线程 进程是资源分配的基本单位,进程的调度涉及到的内容比较多(存储空间,CPU,I/O资源等,进程现场保护),调度开销较大,在并发的切换过程效率较低。为了更高效的进行调度,提出了比进程更轻量的独立运行和调度的基本单位。 线程比进程更轻量 线程能独立运行,独立调度,拥有资源(一般是CPU资源,程序计数器等) 线程调度能大幅度减小调度的成本(相对于进程来说),线程的切换不会引起进程的切换 线程的引入进一步提高了操作系统的并发性,线程能并发执行

How to call Kotlin suspending coroutine function from Java 7

你说的曾经没有我的故事 提交于 2020-03-17 10:50:13
问题 I'm trying to call Kotlin function from Java 7. I'm using coroutines and this called function is suspending, for example: suspend fun suspendingFunction(): Boolean { return async { longRunningFunction() }.await() } suspend fun longRunningFunction() : Boolean { delay(400) return true } I was using coroutines in version 0.25.3 and I could emulate simple Java callback style by passing Continuation<U> instance as an argument to suspending function, e.g. CoroutinesKt.suspendingFunction(new

tornado学习笔记day08-tornado中的异步

我怕爱的太早我们不能终老 提交于 2020-03-17 10:06:08
某厂面试归来,发现自己落伍了!>>> 概述 应为epoll主要用来解决网络的并发问题,所以tornado中的异步也是主要体现在网络的IO异步上,即异步web请求 tornado.httpclient.AsyncHTTPClient tornado提供异步web请求客户端,可以用来进行异步web请求, 这个客户端和服务端是相对来说的,当tornado的Handler去其他位置去请求资源的时候,他就是客户端 fetch(request, callback=None) 用于执行一个web请求,并异步响应返回一个tornado.httpclient.httpresponse request可以是一个url,也可以是一个tornado.httpclient.httprequest对象 如果插入的是url会自动生成一个request对象 HTTPRequest 概述 HTTP请求类,该类的构造函数可以接收参数 参数 url: 字符串类型,要访问的网址,必传 method: 字符串类型,HTTP请求方式 headers: 字典类型,或者HTTPHeaders类型 body: HTTP请求体 HTTPResponse 响应类 属性 code: 状态码 reason: 状态码的描述 body: 响应的数据 error: 异常 @tornado.web.asynchronous 不关闭通讯的通道 ''

一种在C语言中用汇编指令和 System V ucontext 支撑实现的协程切换

天大地大妈咪最大 提交于 2020-02-28 03:23:53
1 实现内容 此文在看了 python yield 和 yield from 机制后,觉得 这种轻量级编程技术在小并发场景下优雅可爱,在大并发场景下较进程或线程而言能突破资源瓶颈 ,实在令人忍不住而想在C语言中实现一个。 然后经过一些学习后,此文就在 Linux 上用C语言实现了一个。目前具体包括 [1] co_yield() —— 类似 python 的 yield,用于协程切换; [2] co_send() —— 类似 python 生成器中的 send(),用于开始或恢复协程的执行; [3] co_yield_from() —— 类似 python 的 yield from,用于同步基于 co_yield() 切换的协程; [4] co_loop() —— 略似于 python 的 asyncio.loop(),用于协程并发调度。 e.g. /** ** brief: suspending current coroutine related with ci then switch to specific coroutine when co_yield() called. ** param: ci, bears control-information for corresponding coroutine. ** return: zero returned if

Parallel request with Retrofit, Coroutines and Suspend functions

删除回忆录丶 提交于 2020-02-26 08:32:07
问题 I'm using Retrofit in order to make some network requests. I'm also using the Coroutines in combination with 'suspend' functions. My question is: Is there a way to improve the following code. The idea is to launch multiple requests in parallels and wait for them all to finish before continuing the function. lifecycleScope.launch { try { itemIds.forEach { itemId -> withContext(Dispatchers.IO) { itemById[itemId] = MyService.getItem(itemId) } } } catch (exception: Exception) { exception

Unity coroutines stop while in background

倾然丶 夕夏残阳落幕 提交于 2020-02-20 05:34:00
问题 my question is the following: currently i have several coroutines running in my game for android/iOS but when i send the game to background in order to try other things with the phone those coroutines stop and only resume after i get back to the game; is there any way to make the coroutines continue running while the game is in background? 回答1: Android will suspend your application by design. Co-routines run in the same thread as your Updates so making a distinction between the two in terms

Unity coroutines stop while in background

你说的曾经没有我的故事 提交于 2020-02-20 05:32:06
问题 my question is the following: currently i have several coroutines running in my game for android/iOS but when i send the game to background in order to try other things with the phone those coroutines stop and only resume after i get back to the game; is there any way to make the coroutines continue running while the game is in background? 回答1: Android will suspend your application by design. Co-routines run in the same thread as your Updates so making a distinction between the two in terms

Suspend function 'callGetApi' should be called only from a coroutine or another suspend function

和自甴很熟 提交于 2020-01-31 04:25:05
问题 I am calling suspended function from onCreate(...) override fun onCreate(savedInstanceState: Bundle?) { ... ... callGetApi() } and the suspended function is:- suspend fun callGetApi() {....} But the error shows up Suspend function 'callGetApi' should be called only from a coroutine or another suspend function 回答1: Suspend function should be called only from coroutine. That means you need to use a coroutine builder, e.g. launch . For example: class Activity : AppCompatActivity(),

What's the difference between loop.create_task, asyncio.async/ensure_future and Task?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-22 05:28:28
问题 I'm a little bit confused by some asyncio functions. I see there is BaseEventLoop.create_task(coro) function to schedule a co-routine. The documentation for create_task says its a new function and for compatibility we should use asyncio.async(coro) which by referring to docs again I see is an alias for asyncio.ensure_future(coro) which again schedules the execution of a co-routine. Meanwhile, I've been using Task(coro) for scheduling co-routine execution and that too seems to be working fine.

Is this implementation of takeWhileInclusive safe?

白昼怎懂夜的黑 提交于 2020-01-22 03:38:06
问题 I found the following implementation of an inclusive takeWhile (found here) fun <T> Sequence<T>.takeWhileInclusive(pred: (T) -> Boolean): Sequence<T> { var shouldContinue = true return takeWhile { val result = shouldContinue shouldContinue = pred(it) result } } The problem is I'm not 100% convinced this is safe if used on a parallel sequence . My concern is that we'd be relying on the shouldContinue variable to know when to stop, but we're not synchronizing it's access. Any insights? 回答1: