简体   繁体   English

QNetworkReply::finished() 信号是顺序调用还是同时调用?

[英]Is QNetworkReply::finished() signal called sequentially or simultaneously?

It's a HTTP request sending method.它是一种 HTTP 请求发送方法。 When the goal website responses, httpFinished() will be called.当目标网站响应时,将调用 httpFinished()。

void HTTPClientBase:: HttpRequestGet()
{
    QNetworkRequest network_request;

    network_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    network_request.setUrl(URL);

    reply = network_manager.get(network_request);
    connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}

void HTTPClientBase::httpFinished()
{
    // process the reply;
}

I can call HttpRequestGet() in a loop.我可以循环调用 HttpRequestGet() 。

static HTTPClientBase myClient;
for(...)
{
  myClient.setUrl(...);
  myClient.HttpRequestGet();
}

Since the the request and response are async, so the loop can be done within an instant time.由于请求和响应是异步的,所以循环可以在瞬间完成。 After several hundreds milliseconds, httpFinished() will be called successively.数百毫秒后,将依次调用 httpFinished()。

I want to know httpFinished() will be called sequentially or simultaneously.我想知道 httpFinished() 将被依次或同时调用。 In other words, if I need to concern simultaneous-programming problems, for example, writing the response datas into a single file.换句话说,如果我需要关注同步编程问题,例如,将响应数据写入单个文件。

The following statements indicate that the httpFinished method will not be executed more than once at the same time:以下语句表明 httpFinished 方法不会同时执行多次:

  • Slots are invoked when no synchronous task is executed.在没有执行同步任务时调用槽。
  • HTTPClientBase is a QObject that lives in a thread so the execution of any of its methods implies that you cannot execute another task. HTTPClientBase 是一个位于线程中的 QObject,因此执行其任何方法都意味着您无法执行另一个任务。
  • The task you do is not concurrence only an asynchronism.您所做的任务不仅仅是并发性。
  • The asynchronism of Qt is valid from a design point of view but actually executes its tasks synchronously. Qt 的异步性从设计的角度来看是有效的,但实际上是同步执行其任务。
  • In one thread only one task can be executed at a time and httpFinished is executed in the thread where HTTPClientBase lives.在一个线程中,一次只能执行一个任务,并且 httpFinished 在 HTTPClientBase 所在的线程中执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM