正在加载数据库?
我的问题是当我连接到 MySQL 数据库时如何执行某些操作? 为了更清楚地说明这一点:让我们说我尝试连接到数据库:
db = MySQLdb.connect(host = "testhost",user ="testuser", passwd ="testpw", db = "testdb")
嗯,一般来说,当主机不是本地主机时,它通常需要一些时间来加载,当这种情况发生时,应用程序“冻结”(我正在使用 wxPython)。现在,我想要的是,它不会显示“冻结”,而是显示代表“加载”的东西,它可以是图像,文本,没关系,而不是冻结。另外,闪屏怎么样?据我尝试,我所做的只是一些闪屏,这些闪屏在 X 次后以及当我点击它时消失。我真的不知道如何利用它来加载资源,例如MySQL数据库。上次我尝试使用闪屏加载 MySQL 数据库时,它实际上先加载,然后显示闪屏(笑)。 谢谢。
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 happens, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢您的帮助,我得到了一个答案,一个针对 wxPython 的更具体的答案。我按照这个简单的例子使它工作:
从这里: http://wiki.wxpython.org/LongRunningTasks ,希望这有帮助:)。
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:
From here: http://wiki.wxpython.org/LongRunningTasks , hope this helps :).
如果您的程序需要同时做两件事,那么使用多线程是一个好方法。就您而言,您有一个想要保持响应能力的 GUI,并且有一个要连接的数据库。您需要在单独的线程中完成数据库工作,而不是在处理 GUI 事件的线程上;这将使 GUI 在建立连接时保持工作。恐怕我无法告诉您如何用 Python 编写多线程代码,但我认为 Google 是您的朋友。
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.
有关如何使用 threading 模块编写多线程 Python 程序的示例,请参阅
http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/
和官方文档:
http://docs.python.org/library/threading.html
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
在这种情况下,线程确实很有帮助
看看这个:http://themattreid.com/ wordpress/2010/08/30/easy-python-threading-mysql-connections/
Threading would really help in this case
Check this out : http://themattreid.com/wordpress/2010/08/30/easy-python-threading-mysql-connections/