Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt通过HTTP传输大文件并刷新到eMMC闪存_Qt_Http_Streaming_Qfile_Qtembedded - Fatal编程技术网

Qt通过HTTP传输大文件并刷新到eMMC闪存

Qt通过HTTP传输大文件并刷新到eMMC闪存,qt,http,streaming,qfile,qtembedded,Qt,Http,Streaming,Qfile,Qtembedded,我在一个内存非常有限的嵌入式Linux设备上,通过HTTP将一个大文件(1Gb)流式传输到Qt中的服务器。当我第一次收到头文件时,我决定将数据写入文件系统的哪个位置,创建一个指向该位置的QFile指针,然后打开该文件进行附加。服务器中有一个“累积”函数,每次新数据到达套接字时都会调用该函数。从这个累积函数中,我希望通过write()将数据流传输到文件中。你可以在下面看到我的累加函数 我的问题是执行此操作时内存的使用--内存不足。我是否应该能够在每次累加迭代中刷新()和fsync(),而不必担心R

我在一个内存非常有限的嵌入式Linux设备上,通过HTTP将一个大文件(1Gb)流式传输到Qt中的服务器。当我第一次收到头文件时,我决定将数据写入文件系统的哪个位置,创建一个指向该位置的QFile指针,然后打开该文件进行附加。服务器中有一个“累积”函数,每次新数据到达套接字时都会调用该函数。从这个累积函数中,我希望通过write()将数据流传输到文件中。你可以在下面看到我的累加函数

我的问题是执行此操作时内存的使用--内存不足。我是否应该能够在每次累加迭代中刷新()和fsync(),而不必担心RAM的使用?我做错了什么?我怎样才能解决这个问题?谢谢-

在使用累积功能之前,我打开了一次文件:

// Open the file
filePointerToWriteTo->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered)
// Extract the QFile pointer from the QVariant
QFile *filePointerToWriteTo = (QFile *)(containerForPointer->pointer).value<void *>();

qDebug() << "APPENDING bytes: " << data.length();

// Write to the file and sync
filePointerToWriteTo->write(data);
filePointerToWriteTo->waitForBytesWritten(-1);
filePointerToWriteTo->flush(); // Flush
fsync(filePointerToWriteTo->handle()); // Make sure bytes are written to disk
下面是累积函数的一部分:

// Open the file
filePointerToWriteTo->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered)
// Extract the QFile pointer from the QVariant
QFile *filePointerToWriteTo = (QFile *)(containerForPointer->pointer).value<void *>();

qDebug() << "APPENDING bytes: " << data.length();

// Write to the file and sync
filePointerToWriteTo->write(data);
filePointerToWriteTo->waitForBytesWritten(-1);
filePointerToWriteTo->flush(); // Flush
fsync(filePointerToWriteTo->handle()); // Make sure bytes are written to disk
//从QVariant中提取QFile指针
QFile*filePointerToWriteTo=(QFile*)(containerForPointer->pointer).value();
qDebug()waitForBytesWrite(-1);
filePointerToWriteTo->flush();//脸红
fsync(filePointerToWriteTo->handle());//确保已将字节写入磁盘
编辑:

我检测了我的代码,“waitForBytesWrited(-1)”调用始终返回“false”。文件说,这应该等到数据写入设备

此外,如果我只取消注释“write(data)”行,那么我的可用内存永远不会减少。会发生什么事?“写”是如何消耗这么多内存的

编辑:

现在我正在做以下工作。我没有耗尽内存,但我的可用内存下降到2Mb,并在那里停留,直到整个文件被传输。此时,内存被释放。如果我杀死了中间的传输,内核似乎保持了内存,因为它保持在2MB左右,直到我重新启动进程并尝试写入同一个文件。我仍然认为我应该能够在每次迭代中使用和刷新内存:

// Extract the QFile pointer from the QVariant
QFile *filePointerToWriteTo = (QFile *)(containerForPointer->pointer).value<void *>();

int numberOfBytesWritten = filePointerToWriteTo->write(data);
qDebug() << "APPENDING bytes: " << data.length() << " ACTUALLY WROTE: " << numberOfBytesWritten;

// Flush and sync
bool didWaitForWrite = filePointerToWriteTo->waitForBytesWritten(-1); // <----------------------- This ALWAYS returns false!
filePointerToWriteTo->flush(); // Flush
fsync(filePointerToWriteTo->handle()); // Make sure bytes are written to disk
fdatasync(filePointerToWriteTo->handle()); // Specific Sync
sync(); // Total sync
//从QVariant中提取QFile指针
QFile*filePointerToWriteTo=(QFile*)(containerForPointer->pointer).value();
int numberOfBytesWrite=filePointerToWriteTo->write(数据);

qDebug(),我可能误解了“free-mt”的输出。我一直在观察输出中的“free”字段,看到它在海量文件传输中下降到2MB左右。我只希望在文件传输完成后,它能恢复到高水平的可用数据。

我认为Linux只是在缓存它所能缓存的一切,并在2MB的可用内存限制周围释放它所能释放的空闲数据。在512 MB RAM系统上接收或发送~2Gb文件时,我不会耗尽内存。在我的Qt程序中,在接收到所有数据后,附加到文件并关闭文件。我在QProcess中执行以下操作,以在单独终端的“free-mt”命令中查看我的“free”内存返回:

// Now we've returned a large file - so free up cache in linux
QProcess freeCachedMemory;
freeCachedMemory.start("sh");
freeCachedMemory.write("sync; echo 3 > /proc/sys/vm/drop_caches"); // Sync to disk and clear Linux cache
freeCachedMemory.waitForFinished();
freeCachedMemory.close();

这是在什么环境下运行的?这是cgi应用程序吗?它是在嵌入式Linux环境中运行的Qt嵌入式应用程序。服务器完全在Qt/C++中,侦听端口80。这是哪个Qt版本?检查write/WaitForBytesWrite/flush的返回值(也打开了,但这与此无关)。遇到了相同的问题。。。在我们的例子中,当内存中满是dentries和inode时,我们的应用程序会被操作系统暂停一段时间(并且没有响应)(我假设直到操作系统将缓存数据刷新到磁盘)。仅供参考,
sync&&sysctl-w vm.drop\u caches=2也可以使用。