Help with threads (I think)

Hi everyone,

Good to be subscribed again, I was subscribed about 4 or 5 years ago. wxPython newbie here.

I've done some googling on my problem but unfortunately I haven't been able to find anything to help me.

I have a little app that downloads a file when you press a button. Currently of course the button just stays pressed until the download finishes. I want the download to start, then let me do something else, like maybe have an animation or something running on the side to indicate that something is happening, then once the download completes I want it to tell me that, and then I will change the indicator to say "Download complete" or something similar.

Now I'm sure this isn't very difficult to accomplish but I'm really a bit stuck. Any suggestions will be appreciated. I will post code samples etc. if needed.

Regards,
Wayne

Wayne,

Robin saw your request and used the Time Machine again…
You’ll find the bits of code you need already one your machine if you downloaded The Demo. They are conveniently located in the “Processes and Events” part of the Demo. You’ll find there examples for starting threads and for IPC.

Peter.

···

On 10/5/07, Wayne Koorts wayne@wkoorts.com wrote:

Now I’m sure this isn’t very difficult to accomplish but I’m really a
bit stuck. Any suggestions will be appreciated. I will post code
samples etc. if needed.


There is NO FATE, we are the creators.

Peter Damoc wrote:

···

On 10/5/07, *Wayne Koorts* <wayne@wkoorts.com > <mailto:wayne@wkoorts.com>> wrote:

    Now I'm sure this isn't very difficult to accomplish but I'm really a
    bit stuck. Any suggestions will be appreciated. I will post code
    samples etc. if needed.

Robin saw your request and used the Time Machine again....
You'll find the bits of code you need already one your machine if you downloaded The Demo. They are conveniently located in the "Processes and Events" part of the Demo. You'll find there examples for starting threads and for IPC.

Thanks for the response Peter. I have looked in the demo (usually my first try - always open on my PC when I'm coding) but I guess if you say it's in there then I haven't looked hard enough. I'll have another look, thanks.

Regards,
Wayne

Robin saw your request and used the Time Machine again....
You'll find the bits of code you need already one your machine if you downloaded The Demo. They are conveniently located in the "Processes and Events" part of the Demo. You'll find there examples for starting threads and for IPC.

Thanks for the response Peter. I have looked in the demo (usually my first try - always open on my PC when I'm coding) but I guess if you say it's in there then I haven't looked hard enough. I'll have another look, thanks.

I've had a look through and done some more googling / python doco / soul searching etc. and I'm still having a problem. Can anyone tell me why the following doesn't work?

    def downloadVid(self, e):
        thread.start_new_thread(self.bgDownloadProc, ())
        while self.lockDL.locked():
            self.gagProgress.Pulse()
            sleep(1)

    def bgDownloadProc(self):
        vidID = self.getVidID(self.txtinURL.GetValue())
        flvURL = vidPrefix + vidID
        self.txtStatus.SetLabel("Downloading...")
        self.lockDL = thread.allocate_lock()
        myURLData = urllib2.urlopen(flvURL)
        flvDownload = open(outputDir + outputFName, "wb")
        flvDownload.write(myURLData.read())
        self.txtStatus.SetLabel("Complete")
        self.lockDL.release()

It just tells me that I'm trying to release a lock that's already been released. All I want is for the progress bar to pulse while the file is downloading then stop at the end.

Regards,
Wayne

Robin saw your request and used the Time Machine again....
You'll find the bits of code you need already one your machine if you downloaded The Demo. They are conveniently located in the "Processes and Events" part of the Demo. You'll find there examples for starting threads and for IPC.

Thanks for the response Peter. I have looked in the demo (usually my first try - always open on my PC when I'm coding) but I guess if you say it's in there then I haven't looked hard enough. I'll have another look, thanks.

I've had a look through and done some more googling / python doco / soul searching etc. and I'm still having a problem. Can anyone tell me why the following doesn't work?

   def downloadVid(self, e):
       thread.start_new_thread(self.bgDownloadProc, ())
       while self.lockDL.locked():
           self.gagProgress.Pulse()
           sleep(1)

   def bgDownloadProc(self):
       vidID = self.getVidID(self.txtinURL.GetValue())
       flvURL = vidPrefix + vidID
       self.txtStatus.SetLabel("Downloading...")
       self.lockDL = thread.allocate_lock()
       myURLData = urllib2.urlopen(flvURL)
       flvDownload = open(outputDir + outputFName, "wb")
       flvDownload.write(myURLData.read())
       self.txtStatus.SetLabel("Complete")
       self.lockDL.release()

Not sure if this will work with what you are doing but it might help to use a timer for pulsing the gauge, then you wont be calling sleep on the main thread and there will be no need for the lock that is giving the error you mentioned below.

def __init__(Somthing):
.
  self._timer = wx.Timer(self)
  self.Bind(wx.EVT_TIMER, self.OnTime)

def OnTime(self, evt):
  self.gagProgress.Pulse()

def downloadVid(self, e):
        self._timer.Start(100)
        thread.start_new_thread(self.bgDownloadProc, ())

def bgDownloadProc(self):
        vidID = self.getVidID(self.txtinURL.GetValue())
        flvURL = vidPrefix + vidID
        self.txtStatus.SetLabel("Downloading...")
        myURLData = urllib2.urlopen(flvURL)
        flvDownload = open(outputDir + outputFName, "wb")
        flvDownload.write(myURLData.read())
        self.txtStatus.SetLabel("Complete")
        wx.CallAfter(self._timer.Stop)

···

On Oct 5, 2007, at 3:23 AM, Wayne Koorts wrote:

It just tells me that I'm trying to release a lock that's already been released. All I want is for the progress bar to pulse while the file is downloading then stop at the end.

Regards,
Wayne

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Cody

I've had a look through and done some more googling / python doco / soul searching etc. and I'm still having a problem. Can anyone tell me why the following doesn't work?

   def downloadVid(self, e):
       thread.start_new_thread(self.bgDownloadProc, ())
       while self.lockDL.locked():
           self.gagProgress.Pulse()
           sleep(1)

   def bgDownloadProc(self):
       vidID = self.getVidID(self.txtinURL.GetValue())
       flvURL = vidPrefix + vidID
       self.txtStatus.SetLabel("Downloading...")
       self.lockDL = thread.allocate_lock()
       myURLData = urllib2.urlopen(flvURL)
       flvDownload = open(outputDir + outputFName, "wb")
       flvDownload.write(myURLData.read())
       self.txtStatus.SetLabel("Complete")
       self.lockDL.release()

Not sure if this will work with what you are doing but it might help to use a timer for pulsing the gauge, then you wont be calling sleep on the main thread and there will be no need for the lock that is giving the error you mentioned below.

That did the trick perfectly, thanks Cody!!

Regards,
Wayne