event-loop

Event Queuing in NodeJS

泄露秘密 提交于 2021-02-19 08:26:18
问题 NodeJS uses a event driven model in which only one thread executes the events. I understand the first event executed will be the user JS code. Simple example from nodeJS website of a webserver is below var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); First event executed will perform the above steps. Later on

Correct mental model for a Javascript async function's 'await': generator's 'yield' vs. 'promise.then()'?

早过忘川 提交于 2021-02-18 07:43:12
问题 Which of a generator's yield vs. promise.then() is a more* correct mental model for understanding 'await'? Property comparison, inferred by stepping through the snippet below with a debugger: await: await does not pause/suspend the running async function’s execution. (The running async function ‘runs to completion’, returning a pending promise when the interpreter hits the 1st await. It’s then immediately removed from the call stack.) await waits for the promise to settle. await expression

How to queue a (macro)task in the JavaScript task queue?

时光总嘲笑我的痴心妄想 提交于 2021-02-16 05:29:58
问题 What is the most straight-forward and direct way to queue a task in the browser event loop, using JavaScript? Things that don't work: window.setImmediate(func): Non-standard. window.setTimeout(func, 0) / window.setInterval(func, 0) : Browsers throttle timers to ≥ 4ms. new Promise(r => r()).then(func) : That queues a microtask, not a task. 回答1: MessagePort.postMessage does just that. onmessage = e => handleMessage; postMessage("","*"); You can even use a MessageChannel if you want a less

node js - what happens to incoming events during callback excution

≡放荡痞女 提交于 2021-02-12 11:36:13
问题 Suppose I have a callback with some heavy synchronous processing. During the execution, the event loop is not free to poll for incoming events. So what happens to these events? are they queued somewhere to be processed later, or are they simply lost? Thanks. 回答1: They are added to queue and processed later: A JavaScript runtime contains a message queue, which is a list of messages to be processed. A function is associated with each message. When the stack is empty, a message is taken out of

C Windows API determine if user inactive for certain period

吃可爱长大的小学妹 提交于 2021-02-11 15:45:56
问题 So I've created a basic program with a blocking message event loop (to use little to no CPU while waiting) and waits for a user to change the foreground window, then executes some code: #include <Windows.h> VOID ExitFunction() { // Do Something } BOOL WINAPI HandlerRoutine(DWORD dwCtrlType) { switch (dwCtrlType) { case CTRL_SHUTDOWN_EVENT: ExitFunction(); return TRUE; case CTRL_LOGOFF_EVENT: ExitFunction(); return TRUE; //default: //We don't care about this event //Default handler is used }

Can a setImmediate() function scheduled in an I/O callback recalculate timeout for other I/O notifications?

萝らか妹 提交于 2021-02-11 12:12:43
问题 There is the poll stage of the Node js's event loop. Its aim is to blockingly wait for I/O notifications and then execute needed callbacks. A timeout time of the waiting is calculated before entering this stage. If there were operations scheduled through setImmediate than the timeout is set at 0, if timers took place than timeout is set with a view of them and if there are no such impediments the blocking would continue "forever". What will happen if the timeout is initially set at "infinity"

Vertx web-server uses only one event-loop thread while 16 are available

▼魔方 西西 提交于 2021-02-08 13:44:16
问题 I use Vert.x v3.5.1. There is the simplest sample of code: vertx.createHttpServer() .requestHandler(anyRouter::accept) .listen(8080); In my case the event loop group size is 16 so I expect that my requests will affect 16 threads. Server is started succesfully but it works only in one thread . (I sent request using different tcp-connections so keep-alive is not a reason here.) Class HttpServerImpl contains httpHandlerMgr and this manager handles a pool of event loops (named availableWorkers ).

Monitoring the size of the Netty event loop queues

你。 提交于 2021-02-07 11:28:11
问题 We've implemented monitoring for the Netty event loop queues in order to understand issues with some of our Netty modules. The monitor uses the io.netty.util.concurrent.SingleThreadEventExecutor#pendingTasks method, which works for most modules, but for a module that handle a few thousand HTTP requests per second it seem to be hung, or very slow. I now realize that the docs strictly specify this can be an issue, and I feel pretty lame... so I'm looking for another way to implement this

Qt: Is there notification when event loop starts?

蓝咒 提交于 2021-02-07 04:54:25
问题 I have a Qt application with this kind of main()... int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow mainWin; ... A separate, non-GUI thread is launched here mainWin.Init(); mainWin.show(); app.exec(); } This other thread that is created before the mainWin needs to know when it can start communicating with the mainWin. But since the mainWin uses Qt signals, slots, timers, etc, it's not truly ready to rock until the event loop is running (via exec()). My question is:

How can I create an asynchronous program with urwid and asyncio?

☆樱花仙子☆ 提交于 2021-01-29 04:23:17
问题 I want to build a chatroom with aiortc. Frist of all, I want to build a mockup with urwid as cli and asyncio. The urwid part is already running fine, user input is possible. I know want to run a coroutine that generates random text and works as chat clients texting in that chatroom. I have tried to run my urwid function with the mainloop as an asyncio coroutine but without success. I don't know how to integrate an asynchronous function into my urwid mainloop. def unhandled(key): """ functin