我在 Qt 中有一个 Web 服务器,它将读取一个非常大的(~1Gb)文件并通过 QTcpSocket 将数据返回给请求者。此套接字由主服务器线程创建。我想使用 QtConcurrent 将此套接字移交给工作线程并将数据发送回那里。
// Using QtConcurrent
BackgroundConcurrent childThreadToReturnLotsOfData;
QFuture<void> futureObject = QtConcurrent::run(&childThreadToReturnLotsOfData, &BackgroundConcurrent::returnPartialLargeFile, resp , &fileToCheckExistence);
我的“returnPartialLargeFile”函数如下所示:
void BackgroundConcurrent::returnPartialLargeFile( QHttpResponse *resp , QFile *fileToCheckExistence ) const
{
// We need an event loop for the QTCPSocket slots
QEventLoop loop;
//QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
// Execute our event loop
//loop.exec();
// To do this in another thread from the one created, you must
// move that socket to this thread, reparent and move
resp->m_connection->m_socket->setParent( 0 );
resp->m_connection->m_socket->moveToThread( QThread::currentThread() );
// Read in chunks until we have sent all data back to the requestor
QByteArray dataToWriteToSocket; // Store the data to send back
while ( dataReadFromFileInBytes > 0 ) {
// Read some Data into the byte array
// Write each chunk to the socket
resp->write(dataToWriteToSocket); // <----- Here is our data from the content of the file
resp->flushData(); // <----- Flush data to the socket
}
// End our response and close the connection
resp->end();
return;
}
我得到的错误是,如果我将 'loop.exec()' 行注释掉,我会收到以下错误:
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread c2630. Receiver '' (of type 'QTcpServer') was created in thread 910a8", file kernel/qcoreapplication.cpp, line 501
如果我取消注释,那么我的函数会在 exec() 行短路,并且永远不会向套接字写入数据,但是我没有收到任何错误,我只是得到一个不包含来自 while 循环的任何数据的截断响应.
我正在重新设置套接字并将其移至新线程,因此我希望我的问题仅与事件循环以及套接字信号和插槽有关。关于我在这里做错了什么的任何想法?我怎样才能让它工作?如果信号/插槽问题,我需要在这里连接哪个?谢谢 -