-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

3

4 Answers 4

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/

Sign up to request clarification or add additional context in comments.

1 Comment

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 !
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') 

1 Comment

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
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/

Comments

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() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.