PDA

View Full Version : How should I thread these QProcesses?



last2kn0
5th October 2007, 01:52
I have a program that I'm using a QProcess to call wget to download thousands of files.

If I were to start a thread for each file to download it, could I do it without explicitly creating QThread objects? For example:


For i from 0 to 10
myThread(i);
End for

Would this work or would the thread get destroyed when it goes to the next iteration of the for loop? Basically the files I'm working with are numbers so I just send it i.

I ask this because if i did something like this:


For i from 0 to 10
myThread object(i);
End for

Wouldn't this cause conflicts? Wouldn't the object get destroyed possibly before it finishes or it would be assigned a new file to download? As you can tell, this is my first time dealing with threads!

Thank you so much for your help!

jpn
5th October 2007, 09:36
QProcess executes an external process. There is no need to run QProcess in a separate thread. QProcess doesn't block anyhow unless you call one of QProcess::wait*() methods.

last2kn0
5th October 2007, 13:21
Okkk, so this my requirements:
I need the process to be running in a specific directory...

If I'm running just QProcesses in that for loop, the object is going to be set to download a new file before its done with the last one unless waitForFinished() is used! Therefore, the gui will be unresponsive.

I can't just call a temporary like this QProcess().start() because I can't set its directory.

jpn
5th October 2007, 13:57
Blocking applications event loop causes nothing to get updated meanwhile. You should just launch the process and connect to its signals instead of using blocking wait-methods. You will get informed via signals when the process is finished etc. You can download next file in corresponding slot.

Btw, have you noticed QHttp::get()? :)

last2kn0
5th October 2007, 14:53
Ok well I'm going to re-implement it using QHttp::get() to see how things work out. Just a few initial questions:

Can I set where the files are downloading to? I want them to go to QDir::tempPath()
Is there someway to get multiple files? Or will I just have to use a for loop?
Wouldn't I have to have that for-loop not progress until the file finishes?

Sorry for all the questions... Its hard to experiment on my own and code it with all my schoolwork! IT SUCKS!:mad:

jpn
5th October 2007, 16:30
Can I set where the files are downloading to? I want them to go to QDir::tempPath()
Notice the second parameter of QHttp::get(). You can easily construct temporary files with QTemporaryFile.


Is there someway to get multiple files? Or will I just have to use a for loop?
Wouldn't I have to have that for-loop not progress until the file finishes?
Yes, you can schedule multiple files to be downloaded. QHttp::get() is asynchronous and returns a unique identifier for each request:

The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished().


Its hard to experiment on my own and code it with all my schoolwork! IT SUCKS!:mad:
"It" as school work or "it" as information technology? :p I warmly suggest putting school work on higher priority than your own personal hacking still if the latter is much more fun. I have myself put way too much effort on the latter in the past.. ;)

last2kn0
6th October 2007, 04:27
Ok, wow that sounds great! (I love Qt) Thanks for the pointers!

Um, so I guess I could just have Qhttp::get go to a QFile within QDir::tempPath(). I think I can handle that.

Yeah... ECSE (Electrical, Computer and Systems Engineering)... so its a fairly heavy workload. I've got my priorities straight... Trying to absorb from everyone else's experience so I can concentrate on my school for now. ;)

I appreciate the help... I've seen it all the time, "Go search for yourself, google's your friend, Qt Assistant, etc..." Thats just not plausible all the time, especially if your crazy and you are trying to dual! ha! I tried a little searching on my own but didn't want to interfere with school. So again, thanks! I'll keep ya updated on the progress when i get a chance to write the code.

last2kn0
9th October 2007, 04:49
I get this when I try to download something with QHttp::get:
QSslSocket: cannot find ssleay32 library: QLibrary::load_sys: Cannot load ssleay
32 (The specified module could not be found.).

My current code is like this:

for (unsigned int i = STARTFILE; i < MAXFILES; ++i){
QString fileName(QString::number(i) + "-AWM-Fixed.rtf");
QFile* file = new QFile(QDir::tempPath() + "/" +fileName);
ul->get(fileName,file);
}

I understand that the QIODevice has to be valid for the duration so this could cause problems. How can I fix this?

Also, I commented out the for loop just to prevent any problems with that... and I still have a problem: The files I download are 0 bytes! Why is this?

Thank you

last2kn0
9th October 2007, 04:56
I would still like an answer to the "ssleay32" thing as well as why the files are 0 bytes but I think I'm going to trash that implementation.

I may create a class like stated here: http://www.qtcentre.org/forum/f-qt-programming-2/t-qhttp-problemdownload-multi-file-7267.html/?highlight=qhttp

That might be a better approach.