event-loop

What is the random factor in node v10 event loop?

对着背影说爱祢 提交于 2020-05-26 09:31:32
问题 My question is about nodejs event loop Consider this code (async () => { let val = 1 const promise = new Promise(async resolve => { resolve() await new Promise(async r => { setTimeout(r) }) await promise val = 2 }) await promise await new Promise(resolve => setTimeout(resolve)) console.log(val) })() With node 10.20.1 (latest version of node 10) for ((i = 0; i < 30; i++)); do /opt/node-v10.20.1-linux-x64/bin/node race-timeout.js; done With node 12.0.0 (first version of node 12) for ((i = 0; i

Is there a faster way to yield to Javascript event loop than setTimeout(0)?

流过昼夜 提交于 2020-05-25 08:26:51
问题 I am trying to write a web worker that performs an interruptible computation. The only way to do that (other than Worker.terminate() ) that I know is to periodically yield to the message loop so it can check if there are any new messages. For example this web worker calculates the sum of the integers from 0 to data , but if you send it a new message while the calculation is in progress it will cancel the calculation and start a new one. let currentTask = { cancelled: false, } onmessage =

event loop prefers microtask queue over callback queue?

大兔子大兔子 提交于 2020-05-23 11:09:08
问题 I was testing out concepts of asynchronous code in JS . Got confused between callback queue & microtask queue order. Whenever promise objects gets resolved , the fulfillment method { then } is pushed into microtask queue while the callbacks of browser timer functions such as setTimeout is pushed into callback queue. Event loop continuously checks queue and pushes functions from queue into call stack whenever call stack gets empty. Event loop should prefer microtask queue over normal callback

event loop prefers microtask queue over callback queue?

此生再无相见时 提交于 2020-05-23 11:09:06
问题 I was testing out concepts of asynchronous code in JS . Got confused between callback queue & microtask queue order. Whenever promise objects gets resolved , the fulfillment method { then } is pushed into microtask queue while the callbacks of browser timer functions such as setTimeout is pushed into callback queue. Event loop continuously checks queue and pushes functions from queue into call stack whenever call stack gets empty. Event loop should prefer microtask queue over normal callback

Async function with +=

*爱你&永不变心* 提交于 2020-02-12 08:07:25
问题 let x = 0; async function test() { x += await 5; console.log('x :', x); } test(); x += 1; console.log('x :', x); The values of x logged are 1 and 5 . My question is: why is the value of x 5 on second log? If the test is executed after x += 1 (since it is an async function) then the value of x is 1 by the time test is executed, so x += await 5 should make the value of x 6 . 回答1: TL;DR: Because += reads x before, but writes it after it has changed, due to await . async functions run

Why are my custom graphical items constantly repainting in a Qt-based C++ GUI application?

别说谁变了你拦得住时间么 提交于 2020-01-15 03:26:09
问题 My application has a QMdiArea, inside which subwindows are shown which contain instances of QGraphicsView-derived views (GfxInteractiveView), which in turn visualize scenes containing custom QGraphicsItem-derived items. /// An image item which is displayed as the background of a scene. class GfxImageItem : public QObject, public QGraphicsPixmapItem { Q_OBJECT protected: GfxInteractiveImgView *view_; QPixmap pixmap_; QList<GfxPointItem *> pointItems_; public: GfxImageItem(GfxInteractiveImgView

RuntimeError: There is no current event loop in thread 'Dummy-1'

*爱你&永不变心* 提交于 2020-01-14 14:47:51
问题 I am working on a web application with the backend in Python and a Django server. I have a few raspberry pis which are sending data to a server and then I am supposed to get those data from my backend. I succeeded in doing it out of my project so I am pretty sure about the code. Now I wanna integrate this function to my project so here is the file : loop = asyncio.get_event_loop() class StartApp: def __init__(self, interval=1): self.interval = interval Mqtt = multiprocessing.Process(target =

What is the relationship between event loop and Promise [duplicate]

主宰稳场 提交于 2020-01-11 18:54:00
问题 This question already has answers here : Promise vs setTimeout (5 answers) Closed 2 years ago . I am curious about the relationship between Event Loop and Promise. The demo exposes the question. I expected the p1 fulfilled appear in the middle, since they queue a task to the same task queue and are executed one by one. var p1 = new Promise(function(resolve, reject){ resolve(1) }) setTimeout(function(){ console.log("will be executed at the top of the next Event Loop") },0) p1.then(function

Prevent NodeJS from exiting event-loop

删除回忆录丶 提交于 2020-01-03 05:22:08
问题 Hey hava googled and tried every thing - but nothing seems to work, so now i will ask you guys: How do i make my Node app. from stop exiting - when i like it to keep running and wait for response from a Web Socket connection? As you can see i have tried different stuff in the 'PreventExit' function. I simply want the app. to keep receiving data from the web socket connection - without closing/exiting. var Gdax = require('gdax'); var WSemitter = require('../eventemitters/websocket-emitter');

Python event loop does not work properly with stdin

你说的曾经没有我的故事 提交于 2020-01-02 19:32:50
问题 I am trying to get my head around python's asyncio. I have written this piece of code just for demonstration to clear concept. import asyncio import threading async def printer(b, a): print(b) await asyncio.sleep(5) print(a) def loop_runner(loop): print('[RUNNING LOOP]') loop.run_forever() if __name__ == '__main__': event_loop = asyncio.get_event_loop() # run_forever() is blocking. running it from separate thread loop_thread = threading.Thread(target=loop_runner, args=(event_loop,)) loop