coroutine

Scanner and parser interaction

爱⌒轻易说出口 提交于 2019-12-11 05:20:00
问题 I am new to flex/bison. Reading books, it seems that in nearly all compiler implementations, the parser interacts with the scanner in a "coroutine" manner, that whenever the parser needs a token, it calls the scanner to get one, and left the scanner aside when it's busy on shift/reduce. A natural question is that why not let the scanner produces the token-stream (from the input byte-stream) as a whole, and then pass the entire token-stream to the parser, thus there is no explicit interaction

How to unit test coroutine when it contains coroutine delay?

百般思念 提交于 2019-12-11 04:25:43
问题 When I add a coroutine delay() in my view model, the remaining part of the code will not be executed. This is my demo code: class SimpleViewModel : ViewModel(), CoroutineScope { override val coroutineContext: CoroutineContext get() = Dispatchers.Unconfined var data = 0 fun doSomething() { launch { delay(1000) data = 1 } } } class ScopedViewModelTest { @Test fun coroutineDelay() { // Arrange val viewModel = SimpleViewModel() // ActTes viewModel.doSomething() // Assert Assert.assertEquals(1,

Python: I don't understand what's happening with this generator

蹲街弑〆低调 提交于 2019-12-11 03:47:25
问题 I'm curious as to what's happening here. Can someone who knows generators and coroutines well explain this code. def b(): for i in range(5): yield i x = (yield) print(x) def a(): g = b() next(g) for i in range(4): g.send(5) print(next(g)) a() output None 1 None 2 None 3 None 4 but when I switch around lines 3 and 4: the lines yield i and x = (yield) , I get the following. 5 None 5 None 5 None 5 None I suspect the problem might me from trying to use the yield statement to both receive and send

How to run suspend method via reflection?

荒凉一梦 提交于 2019-12-11 02:21:29
问题 There is an coroutine block that can runs suspend functions. But I call the function by invoke via reflection. This is java style invocation, apparently a simple call will not work. Are there ways to run reflected method asynchronously? How to await this method? import kotlin.coroutines.experimental.* class TestClass(val InString: String) { suspend fun printString() { println(InString) } } fun launch(context: CoroutineContext, block: suspend () -> Unit) = block.startCoroutine

How await keyword works?

萝らか妹 提交于 2019-12-10 19:49:46
问题 For a given below co-routine( f ), import csv import urllib def f(resp): print('Line 1') yield csv.reader(resp.read().decode('utf-8')) def h(): url = 'http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NYSE&render=download' resp = urllib.urlopen(url) cr = f(resp) cr = f(resp) assigns an iterator object to cr , cr.next() execute Line 1 and block at yield keyword. My understanding is, with syntax cr=f(resp) there is no event-loop(task scheduler) with threading, behind the scene

How can I use coroutines with volley so that my code can be written like sychronous?

依然范特西╮ 提交于 2019-12-10 15:25:30
问题 Here's an example from developer.android.com class MainActivity : AppCompatActivity() { lateinit var textView:TextView lateinit var button:Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textView = findViewById(R.id.textView) button = findViewById(R.id.button) button.setOnClickListener({ getData() }) } fun getData(){ val queue = Volley.newRequestQueue(this) val url = "http://www.google.com/" val

libcoro:在c++中支持coroutine

吃可爱长大的小学妹 提交于 2019-12-10 14:52:47
起因 在第一个版本的 libtnet 开发完成之后,我一直在思考如何让异步方式的网络编程更加简单。 虽然 libtnet 通过c++ shared_ptr以及function等技术很大程度上面解决了异步代码编写的一些问题,但是仍然会出现代码逻辑被强制拆分的情况。而这个则是项目中童鞋无法很好的使用其进行开发的原因。 所以我考虑让 libtnet 支持coroutine。 Coroutine 第一次接触coroutine的概念是在lua里面,记得当时想了很久才算弄明白了coroutine的使用以及原理。在lua中,coroutine的使用如下: co = coroutine.create(function () print("begin yield") coroutine.yield() print("after yield") end) coroutine.resume(co) print("after resume") coroutine.resume(co) 我们可以通过resume执行一个新创建或者已经被挂起的coroutine,通过yield挂起当前的coroutine,这样就可以实现类似多线程方式下面的多任务调度。 至于coroutine的原理,很多地方都有说明,主要就在于每个coroutine都有自己的堆栈,这样当coroutine挂起的时候,它的当前执行状态会被完整保留

How to implement coroutine within for loop in c

梦想的初衷 提交于 2019-12-10 11:44:18
问题 Here is part code: void a() { printf("entering a\n"); int i; for(i = 0; i < 3; i++){ if(setjmp(a_buf) == 0) { printf("A step %d\n", i); b(); } else { longjmp(b_buf, 1); } } printf("returning from a\n"); } void b() { printf("entering b\n"); int i; for(i = 0; i < 5; i++){ if(setjmp(b_buf) == 0) { printf("B step %d\n", i); a(); } else { longjmp(a_buf, 1); } } printf("returning from b\n"); } I have two processes a & b . How to make them works as coroutine. Wish them doing A Step 0 then B Step 0

Will a Python generator be garbage collected if it will not be used any more but hasn't reached StopIteration yet?

家住魔仙堡 提交于 2019-12-09 07:55:38
问题 When a generator is not used any more, it should be garbage collected, right? I tried the following code but I am not sure which part I was wrong. import weakref import gc def countdown(n): while n: yield n n-=1 cd = countdown(10) cdw = weakref.ref(cd)() print cd.next() gc.collect() print cd.next() gc.collect() print cdw.next() On the second last line, I called garbage collector and since there is no call to cd any more. gc should free cd right. But when I call cdw.next() , it is still

Wait for the termination of n goroutines

坚强是说给别人听的谎言 提交于 2019-12-09 05:22:57
问题 I need to start a huge amount of goroutines and wait for their termination. The intuitive way seems to use a channel to wait till all of them are finished : package main type Object struct { //data } func (obj *Object) Update(channel chan int) { //update data channel <- 1 return } func main() { channel := make(chan int, n) list := make([]Object, n, m) for { for _, object := range list { go object.Update(channel) } for i := 0; i < n; i++ { <-channel } //now everything has been updated. start