Loading database?
my question is how can I do something while I connect to a MySQL database? To make this clearer: Lets say I try to connect to a database:
db = MySQLdb.connect(host = "testhost",user ="testuser", passwd ="testpw", db = "testdb")
Well, thing is, generally, when the host isn't localhost, it usually takes up a bit to load and while this ha开发者_如何学Goppens, the application "freezes" (I'm using wxPython). Now, what I want is, instead of "freezing", it would display something that represents "loading", it could be an image, a text, doesn't matter, instead of freezing. Also, what about splashscreens? As far as I tried, all I've managed to do is some splashscreens which disappear after X time and when I click on it. I really don't know how to take advantage of it to load resources and, for example, a MySQL database. The last time I tried to load up a MySQL database with splashscreen, it actually loaded up first, then it showed the splashscreen (lol). Thanks.
Thanks for all your help, I came to an answer, a more concrete one for wxPython. I followed this simple example to make it work:
import wx
import thread
from time import sleep
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.label = wx.StaticText(self, label="Ready")
self.btn = wx.Button(self, label="Start")
self.gauge = wx.Gauge(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.label, proportion=1, flag=wx.EXPAND)
sizer.Add(self.btn, proportion=0, flag=wx.EXPAND)
sizer.Add(self.gauge, proportion=0, flag=wx.EXPAND)
self.SetSizerAndFit(sizer)
self.Bind(wx.EVT_BUTTON, self.onButton)
def onButton(self, evt):
self.btn.Enable(False)
self.gauge.SetValue(0)
self.label.SetLabel("Running")
thread.start_new_thread(self.longRunning, ())
def onLongRunDone(self):
self.gauge.SetValue(100)
self.label.SetLabel("Done")
self.btn.Enable(True)
def longRunning(self):
"""This runs in a different thread. Sleep is used to simulate a long running task."""
sleep(3)
wx.CallAfter(self.gauge.SetValue, 20)
sleep(5)
wx.CallAfter(self.gauge.SetValue, 50)
sleep(1)
wx.CallAfter(self.gauge.SetValue, 70)
sleep(10)
wx.CallAfter(self.onLongRunDone)
if __name__ == "__main__":
app = wx.PySimpleApp()
app.TopWindow = MainFrame(None)
app.TopWindow.Show()
app.MainLoop()
From here: http://wiki.wxpython.org/LongRunningTasks , hope this helps :).
If your program needs to do two things at once, then using multiple threads is a good way to do it. In your case, you have a GUI that you'd like to stay responsive, and you have a database to connect to. You need to do the database work in a separate thread, instead of on the thread that handles GUI events; that will keep the GUI working while the connection is being made. I'm afraid I can't tell you how to write multithreaded code in Python, but I think Google is your friend here.
For examples of how to write multithreaded Python programs using the threading module, see
http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/
and the official documentation:
http://docs.python.org/library/threading.html
Threading would really help in this case Check this out : http://themattreid.com/wordpress/2010/08/30/easy-python-threading-mysql-connections/
精彩评论