问题标签 [event-loop]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
116045 浏览

python - Tkinter:如何使用线程来防止主事件循环“冻结”

我有一个带有“开始”按钮和进度条的小型 GUI 测试。期望的行为是:

  • 点击开始
  • 进度条振荡 5 秒
  • 进度条停止

观察到的行为是“开始”按钮冻结 5 秒,然后显示进度条(无振荡)。

到目前为止,这是我的代码:

根据 Bryan Oakley here提供的信息,我知道我需要使用线程。我尝试创建一个线程,但我猜测由于线程是从主线程中启动的,所以它没有帮助。

我的想法是将逻辑部分放在不同的类中,并从该类中实例化 GUI,类似于 A. Rodas此处的示例代码。

我的问题:

我不知道如何对其进行编码,以便此命令:

调用位于另一个类中的函数。这是一件坏事还是有可能?我将如何创建可以处理 self.tb_click 的第二类?我尝试遵循 A. Rodas 的示例代码,该代码运行良好。但是我无法弄清楚如何在触发动作的 Button 小部件的情况下实现他的解决方案。

如果我应该从单个 GUI 类中处理线程,如何创建一个不干扰主线程的线程?

0 投票
2 回答
5960 浏览

javascript - 在浏览器中运行的 JavaScript 的单线程概念

下图摘自Jon Resig的《JavaScript Ninja 的秘密》一书的第 3 章。在这里作者正在解释浏览器事件循环。

在此处输入图像描述

这本书不得不说:

需要注意的是,将事件放入队列的浏览器机制在此事件循环模型之外。确定事件何时发生并将它们推送到事件队列所需的处理不参与处理事件的线程。

所以我的问题是说浏览器中的 JavaScript 是单线程的是否正确?我问这个问题是因为显然两个独立的任务(处理事件和事件队列在这里并行进行)。

0 投票
1 回答
4427 浏览

c++ - 在 Qt 中,当事件循环线程拥有的 QObject 上的插槽正在执行时,QThread 的事件循环是否阻塞?

我想确认一下我认为 Qt 中工作线程的一个简单方面。

假设我创建了一个 QThread,其目的是管理其相应线程中的耗时工作。start()此外,假设我通过调用QThread来允许该线程的相应事件循环运行。工作本身由 QThread 的started()信号指示的成员函数(插槽)执行。

即(从https://stackoverflow.com/a/11039216/368896复制):

我的问题如下。我知道工作线程的事件循环将接收来自finished()信号的触发器,以便调用实例deleteLater()上的插槽。task此外,此事件循环将在函数返回后不久运行doWork(),因此它将准备好并且可用于处理来自刚刚通过调用函数末尾finished()添加到工作线程事件队列的信号的触发器.finished()doWork()

我想确认在内部执行的耗时操作的整个过程中doWork()(在finished()发出之前和doWork()函数退出之前),工作线程内的事件循环在插槽函数上被阻塞doWork(),因此工作线程将在耗时函数的整个执行过程中,不响应事件循环线程拥有的任何对象上触发的任何槽。doWork()(因此,任何此类插槽只会在doWork()退出后、一旦工作线程的事件循环再次处于活动状态以及在finished()处理来自信号的触发器之前执行。)

我怀疑是这种情况,但我想确认一下。

谢谢!

0 投票
2 回答
4755 浏览

javascript - node.js 和 setTimeout 和 setInterval - 理解事件循环

我编写了下面的程序,以努力理解事件循环和 setTimeout 和 setInterval 等函数。

该程序的输出与我的预期不同:

输出是:

问题:

  1. 为什么“驱逐所有人”不是首先执行?
  2. 为什么间隔总是最后执行?
  3. 有人可以解释一下整个程序的执行。
  4. 退出程序前要等待一段时间,为什么?

程序:

0 投票
1 回答
1333 浏览

c++ - 为什么我的自定义图形项在基于 Qt 的 C++ GUI 应用程序中不断重绘?

我的应用程序有一个 QMdiArea,其中显示了包含 QGraphicsView 派生视图 (GfxInteractiveView) 实例的子窗口,这些视图反过来可视化包含自定义 QGraphicsItem 派生项的场景。

到目前为止一切都很好,但我还有另一个项目 GfxPointItem,它是完全自定义的,它具有实际执行某些操作的 paint(),当我将它添加到场景中时,CPU 使用率在其中一个核心上像所有项目一样跳到满在层次结构中输入一个重绘循环,只要窗口可见或场景中存在自定义项,该循环就会持续。发生这种情况时查看堆栈表明我的函数都不负责调用paint(),事件是在事件循环上生成的。这是 GfxPointItem 的代码:

我不知道为什么事件循环不断重新绘制项目。我什至不会注意到这一点,但是我在显示标准 QFileDialog::getExistingDirectory() 时遇到了问题,当带有图形项的窗口可见时,它甚至无法绘制,因为重绘偷走了所有执行时间主线程远离它,导致冻结。之后,我在绘制函数中添加了跟踪语句,并在几秒钟后在日志文件中找到了数万条条目,而应用程序似乎什么也没做。在任务管理器中,当窗口可见时(在 4 核处理器上),CPU 使用率约为 25%,当我关闭它时它降至 0。

我的代码都没有强制这些重绘,那是什么?如何调试事件循环以找到此行为的来源,这会减慢我的应用程序并导致冻结?

谢谢!

Qt 版本是最新的 5.0.2 二进制包,应用程序使用 Visual C++ 2012 for x64 编译。

0 投票
2 回答
1677 浏览

multithreading - QThread:从主线程调用子线程方法

我有一个通过继承创建的线程QThread,我调用它exec()来启动事件循环。而这个继承的类QThread里面有一个方法。

如何从主线程调用该方法以使其在子线程中执行?我假设该方法的执行必须在子线程的事件循环中排队,所以调用threadObject->childThreadMethod()不是一个好主意。有什么解决办法吗?

0 投票
1 回答
2175 浏览

node.js - node.js 中的事件循环是如何实现的?

如果节点只有两个线程,一个用于执行主代码,另一个用于所有回调,如果回调是资源/时间密集型的,则仍然会发生阻塞。

假设您有 100,000 个并发用户,并且对节点应用程序的每个客户端请求都运行一个复杂且耗时的数据库查询,(假设未完成缓存)以后的用户在等待查询返回时会遇到阻塞吗?

如果每个回调都可以在自己的线程上运行,那么这不是问题。但是,如果所有回调都在一个单独的专用线程上运行,那么 node 在这种情况下并不能真正帮助我们。

0 投票
2 回答
3113 浏览

c++ - 基于套接字的事件循环

我想基于套接字设置服务器-客户端通信。客户端可以连接到服务器并从中接收不同的通知。这可以在客户端实现如下

此代码将在客户端的单独线程中运行,并应处理来自服务器的不同类型的通知。但是客户端也应该能够通过发送请求与套接字进行通信,即

问题:如何实现这一目标?我不想中断阅读线程,但我应该收到对特定请求的响应。这是基于本地域流的 unix 套接字。

0 投票
2 回答
3897 浏览

node.js - Node.js 中的单线程和事件循环

首先,我是初学者,试图了解 Node.Js 是什么。我有两个问题。

第一个问题
来自Felix的文章,它说“只能同时触发一个回调。在该回调完成执行之前,所有其他回调都必须排队等待”。

然后,考虑以下代码(从nodejs官网复制)

如果同时收到两个客户端请求,则表示如下工作流程:

  1. 收到第一个 http 请求事件,收到第二个请求事件。
  2. 一旦收到第一个事件,第一个事件的回调函数就会执行。
  3. 同时,第二个事件的回调函数必须等待。

我对吗?如果我是对的,如果在很短的时间内有数千个客户端请求, Node.js如何控制。

第二个问题
“事件循环”一词主要用于 Node.js 主题。我从http://www.wisegeek.com/what-is-an-event-loop.htm将“事件循环”理解为以下内容;

事件循环(或主循环)是程序中的一种结构,用于控制和调度初始事件之后的事件。

初始事件可以是任何东西,包括按下键盘上的按钮或单击程序上的按钮(在 Node.js 中,我认为初始事件将是 http 请求、数据库查询或 I/O 文件访问)。

这称为循环,不是因为事件循环并不断发生,而是因为循环为事件做准备、检查事件、分派事件并重新重复该过程。

我对第二段有冲突,尤其是“从头再来”这句话。我接受上述问题中的上述http.createServer代码绝对是“事件循环”,因为它反复监听 http 请求事件。

但是我不知道如何识别以下代码是事件驱动还是事件循环。除了 db 查询完成后触发的回调函数之外,它不会重复任何内容。

请让我听听你的意见和答案。

0 投票
1 回答
226 浏览

javascript - Javascript about event registration and event loop confused

I tried a long time to figure out, how event and event loop works. I read so many article in internet and books about event registration and event loop, but I still can not imagine how it works.

I am reading the book Async JavaScript from Prgmatic Bookshelft about event queue, you can read in the book an article.

When we call setTimeout, a timeout event is queued. Then execution continues, the code after the setTimeout call runs and so on, until no code more left. After then it will start the event loop and detect, which event get fired and it will call the handler.

After this article, I have got a bit imagination about event and async in Javascript. Then I tried by myself the following code:

I have set the time 0, because I want to have confirmation about this sentence:

the code after the setTimeout call runs and so on..

and

After then it will start the event loop and...

until here, everything is fine how event in Javascript; it works. Then I keep continue reading Async JavaScript book, and had read the following:

Input events work the same way: when a user clicks a DOM element with a click handler attached, a click event is queued. But the handler won't be executed until all currently running code had finished.

The first sentence sounds for me a bit confused. For example:

What I don't understand here is, when the compiler will saved a click event in the event queue? Will it be saved in the queue after registration (after this code) or when I had clicked the element?

In node I tried this code, to figure out how event works. Consider the following code:

What I am expected here is:

I am in the loop
I am in the loop
I am in the loop
I am in the loop
I am in the loop
I am in the loop
I am finish with loop
Into ev1

but what I got it is:

Into ev1
I am in the loop
I am in the loop
I am in the loop
I am in the loop
I am in the loop
I am in the loop
I am finish with loop

After this output, it breaks all my imagination about event (async) execution and event loop that I had read in the book and article in the net before. Consider this sentence in the book:

the code after the setTimeout call runs and so, until no code more left. After then it will start the event loop and detect, which event get fired and it will call the handler.

But why the code above with the EventEmitter works opposite? I thought the event loop will be execute at the end (after other codes be done).

I had read in internet about database (for example mongoose) query with node is async. I don't know how async database query definitively works, but I am imagining like this:

Database request and response

Please explain me, how events in Javascript really work?