-1

ok so one of my functions is to open a url and read its contents and then write it in a file the problem is when i do that my UI freezes i know that i need to use asynchronous downloads but i dont seem to understand exatcly how to ! the url im openining is about 10-20 mg also would http://docs.python.org/library/threading.html help me in any way ? my code :

f = open("hello.txt",'wb')
datatowrite = urllib.urlopen(link).read()
f.write(datatowrite)
f.close()

an example would be much appreciated

Thanks

nmnm
  • 79
  • 2
  • 3
  • possible duplicate of [urllib freeze if url is too big !](http://stackoverflow.com/questions/6565910/urllib-freeze-if-url-is-too-big) – Paul McMillan Jul 03 '11 at 23:30
  • Modify your previous question with the new information, rather than re-asking the same question in a slightly different form. – Paul McMillan Jul 03 '11 at 23:30
  • Also pretty similar to http://stackoverflow.com/questions/668257/python-simple-async-download-of-url-content – Paul McMillan Jul 03 '11 at 23:33

4 Answers4

0

You can use the asynhttp client to do this, since you can't be bothered to read the docs on threading.

http://code.google.com/p/asynhttp/

Paul McMillan
  • 19,693
  • 9
  • 57
  • 71
  • i asked for an example and no one gave me one !, im new to programming and i dont quite understand the page you linked me to ! – nmnm Jul 03 '11 at 23:49
0

Here's an example. The 10 in the call to asyncDownload is the timeout period in seconds. You'll want to increase that, or possibly get rid of it altogether. The download results are stored under thread.dataToWrite.

import threading
import urllib2 as ul

class asyncDownload(threading.Thread):

   def __init__(self,url,http_timeout):
      threading.Thread.__init__(self)
      self.url = url
      self.http_timeout = http_timeout

   def run(self):
      self.dataToWrite = ul.urlopen(self.url,timeout=self.http_timeout).read()


url = 'http://www.yahoo.com'
thread = asyncDownload(url,10)
thread.run()
print('this thread is still running')
joshayers
  • 3,269
  • 4
  • 23
  • 19
  • thank you for the example, i dont know if im missing something, but when i run the code and replace the url to the one that i have, it does not seem to be doing anything more than the usual !, i mean when i run the code it still freezes python and wxpython which is what im trying to prevent, also i played with the time out (10) and that did not seem to change anything, i used time.time() to see if there was a difference in the time it took for the file to be read and it was the same if the timeout was 1 or 100000 ! thanks again for the example though – nmnm Jul 04 '11 at 02:01
0

You need to take that threading example that was given and combine it in a wxPython program. You can use the example on this site and basically modify it slightly to use the new threading example: http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
0

Adapted from http://wiki.wxpython.org/LongRunningTasks

import wx
import thread

class MainFrame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.btn = wx.Button(self, label="Start")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.btn, proportion=0, flag=wx.EXPAND)
        self.SetSizerAndFit(sizer)
        self.Bind(wx.EVT_BUTTON, self.onButton)

    def onButton(self, evt):
        self.btn.Enable(False)
        thread.start_new_thread(self.longRunning, ())

    def onLongRunDone(self):
        print "finished my task, I may want to update GUI elements here"
        self.btn.Enable(True)

    def longRunning(self):
        f = open("hello.txt",'wb')
        datatowrite = urllib.urlopen(link).read()
        f.write(datatowrite)
        f.close()
        wx.CallAfter(self.onLongRunDone)

if __name__ == "__main__":
    app = wx.PySimpleApp()
    app.TopWindow = MainFrame(None)
    app.TopWindow.Show()
    app.MainLoop()
emeth
  • 364
  • 4
  • 8