Results 1 to 9 of 9

Thread: How should I thread these QProcesses?

  1. #1
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How should I thread these QProcesses?

    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:

    Qt Code:
    1. For i from 0 to 10
    2. myThread(i);
    3. End for
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. For i from 0 to 10
    2. myThread object(i);
    3. End for
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How should I thread these QProcesses?

    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.
    J-P Nurmi

  3. #3
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How should I thread these QProcesses?

    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.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How should I thread these QProcesses?

    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()?
    J-P Nurmi

  5. #5
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How should I thread these QProcesses?

    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!

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How should I thread these QProcesses?

    Quote Originally Posted by last2kn0 View Post
    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.

    Quote Originally Posted by last2kn0 View Post
    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().
    Quote Originally Posted by last2kn0 View Post
    Its hard to experiment on my own and code it with all my schoolwork! IT SUCKS!
    "It" as school work or "it" as information technology? 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..
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    last2kn0 (6th October 2007)

  8. #7
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How should I thread these QProcesses?

    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.

  9. #8
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How should I thread these QProcesses?

    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:
    Qt Code:
    1. for (unsigned int i = STARTFILE; i < MAXFILES; ++i){
    2. QString fileName(QString::number(i) + "-AWM-Fixed.rtf");
    3. QFile* file = new QFile(QDir::tempPath() + "/" +fileName);
    4. ul->get(fileName,file);
    5. }
    To copy to clipboard, switch view to plain text mode 

    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

  10. #9
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How should I thread these QProcesses?

    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-p...ighlight=qhttp

    That might be a better approach.

Similar Threads

  1. GUI thread and Working thread comunication
    By FasTTo in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 15:31
  2. Terminating a thread.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2007, 11:14
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.