coroutine

Can I take advantage of Kotlin's Coroutines by using them in Java code?

六月ゝ 毕业季﹏ 提交于 2019-12-04 18:42:34
问题 What's my goal? My goal is to be able to use Kotlin's Coroutine system from Java. I want to be able to pause mid-execution for a given amount of time, and then pick back up at that spot after the given amount of time has passed. From Java, I'd like to be able to execute tasks that allow pausing mid-execution without in an asynchronous fashion, such as: //example 1 someLogic(); pause(3000L); //3 seconds someMoreLogic(); //example 2 while(true) { someContinuedLogic(); pause(10000L); //10

Lua :: How to write simple program that will load multiple CPUs?

无人久伴 提交于 2019-12-04 18:10:53
问题 I haven't been able to write a program in Lua that will load more than one CPU. Since Lua supports the concept via coroutines, I believe it's achievable. Reason for me failing can be one of: It's not possible in Lua I'm not able to write it ☺ ( and I hope it's the case ) Can someone more experienced (I discovered Lua two weeks ago) point me in right direction? The point is to write a number-crunching script that does hi-load on ALL cores... For demonstrative purposes of power of Lua. Thanks..

why my coroutine blocks whole tornado instance?

浪子不回头ぞ 提交于 2019-12-04 15:50:01
问题 from tornado import web, gen import tornado, time class CoroutineFactorialHandler(web.RequestHandler): @web.asynchronous @gen.coroutine def get(self, n, *args, **kwargs): n = int(n) def callbacker(iterator, callback): try: value = next(iterator) except StopIteration: value = StopIteration callback(value) def factorial(n): x = 1 for i in range(1, n+1): x *= i yield yield x iterator = factorial(n) t = time.time() self.set_header("Content-Type", "text/plain") while True: response = yield gen

CoroutineExceptionHandler not executed when provided as launch context

情到浓时终转凉″ 提交于 2019-12-04 12:05:09
When I run this: fun f() = runBlocking { val eh = CoroutineExceptionHandler { _, e -> trace("exception handler: $e") } val j1 = launch(eh) { trace("launched") delay(1000) throw RuntimeException("error!") } trace("joining") j1.join() trace("after join") } f() This is output: [main @coroutine#1]: joining [main @coroutine#2]: launched java.lang.RuntimeException: error! at ExceptionHandling$f9$1$j1$1.invokeSuspend(ExceptionHandling.kts:164) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32) at kotlinx.coroutines.ResumeModeKt.resumeMode(ResumeMode.kt:67)

Error with Room dao class when using Kotlin coroutines

萝らか妹 提交于 2019-12-04 09:34:07
问题 I'm trying to use kotlin coroutines to access room database by the method described here, added the plugin and dependency, and enabled kotlin coroutines in gradle. in gradle file: kotlin { experimental { coroutines 'enable' } } dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21" ...} So I added suspend keyword for all the methods in dao class, like this: dao class @Query("select * from myevent") suspend fun all(): List<MyEvent> @Delete suspend fun deleteEvent

how to use c# threads in unity3d for android platform?

南笙酒味 提交于 2019-12-04 09:21:37
I am in need to load files, scenes and play animations in threads.. Tried loading files via www in Android... how to do other stuff via threads? But how come a game engine doesn't allow us to create threads? or my understanding is wrong? how can one create threads in UNITY3D? You can use threads in Unity but the engine is not thread safe. Usually you run detached threads (from the Unity UI) to do long running processes and check on results (you cannot interact with Unity from the working thread). The common approach is to use a class which represents a threading job which will be initialized

Python gevent学习笔记 2

旧城冷巷雨未停 提交于 2019-12-04 03:57:49
在上一篇里了解了gevent应用的IO模型概念之后,接下来开始真正了解gevent的使用。 Greenlet 在gevent里面最多应用到的就是greenlet,一个轻量级的协程实现。在任何时间点,只有一个greenlet处于运行状态。Greenlet与multiprocessing 和 threading这两个库提供的真正的并行结构的区别在于这两个库会真正的切换进程,POSIX线程是由操作系统来负责调度,并且它们是真正并行的。 同步和异步 应对并发的主要思路就是将一个大的任务分解成一个子任务的集合并且能够让它并行或者异步地执行,而不是一次执行一个或者同步执行。在两个子任务中的切换被称为上下文切换。 gevent里面的上下文切换是非常平滑的。在下面的例子程序中,我们可以看到两个上下文通过调用 gevent.sleep()来互相切换。 import gevent def foo(): print('Running in foo') gevent.sleep(0) print('Explicit context switch to foo again') def bar(): print('Explicit context to bar') gevent.sleep(0) print('Implicit context switch back to bar') gevent

Python与协程从Python2—Python3

不想你离开。 提交于 2019-12-04 03:57:40
协程,又称微线程、纤程,英文名Coroutine;用一句话说明什么是线程的话:协程是一种用户态的轻量级线程。 Python对于协程的支持在python2中还比较简单,但是也有可以使用的第三方库,在python3中开始全面支持,也成为python3的一个核心功能,很值得学习。 协程介绍 协程,又称微线程、纤程,英文名Coroutine;用一句话说明什么是线程的话:协程是一种用户态的轻量级线程。 协程拥有自己的寄存器上下文和栈。协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈。因此:协程能保留上一次调用时的状态(即所有局部状态的一个特定组合),每次过程重入时,就相当于进入上一次调用的状态,换种说法:进入上一次离开时所处逻辑流的位置。 协程的优点: 1)无需线程上下文切换的开销 2)无需原子操作锁定及同步的开销 3)方便切换控制流,简化编程模型 4)高并发+高扩展性+低成本:一个CPU支持上万的协程都不是问题。所以很适合用于高并发处理。 协程的缺点: 1)无法利用多核资源:协程的本质是个单线程,它不能同时将 单个CPU 的多个核用上,协程需要和进程配合才能运行在多CPU上 2)进行阻塞(Blocking)操作(如IO时)会阻塞掉整个程序 Python2中的协程 yield关键字 Python2对于协程的支持,是通过yield关键字实现的

Is it safe to use spawn directly in an asio stackful coroutine?

大城市里の小女人 提交于 2019-12-03 18:00:38
When I use spawn to start a new stackfull coroutine in a coroutine, valgrind says a lot of using uninitialised value( valgrind output ). Then I use io_service.post to invoke a handler,and start a new stackfull coroutine in it, every thing seems fine. I have searched and read some documents, but can't find something about how to create a new stackfull coroutine safely in a stackfull coroutine. Here is the code: #include <iostream> #include <boost/asio.hpp> #include <boost/asio/spawn.hpp> #include <boost/asio/system_timer.hpp> #include <chrono> using namespace std; int main() { auto use_post =

Unity coroutines stop while in background

▼魔方 西西 提交于 2019-12-03 17:04:30
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? 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 of running in the foreground is not too likely. Having said that there are ways around this. You could build