Hi, I am trying to make a SQL client app. (MySQL by pymysql)
I run sql works in a sub-thread to prevent the main frame from freezing, however it seems there is a thread-safety issue.
Even though the app is yielded from the main thread constantly, the app freezes about 10 secs after the sub-thread gets started.
Please understand that I can’t provide an example for sql side.
Anyone has a solution to yield the app?
import threading
import pymysql
import wx
import time
def SQLWork():
'''
con = pymysql.connect(host='', port=10, user='', password='', db='')
cur = con.cursor()
sql = 'UPDATE this_table SET something=%s WHERE PK=%s;'
# THIS LINE TAKES 2-4 MINUTES
# BECAUSE 'something' IS QUITE LONG BLOB
cur.execute(sql, ('something', 'pk'))
con.commit()
'''
time.sleep(10)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Test Frame')
pn = wx.Panel(self)
bt = wx.Button(pn, label='Start Work')
sz = wx.BoxSizer(wx.HORIZONTAL)
sz.AddMany((
((-1, -1), 1),
(bt, 0, wx.ALIGN_CENTER_VERTICAL),
((-1, -1), 1)
))
pn.SetSizer(sz)
bt.Bind(wx.EVT_BUTTON, self.OnBtn)
def OnBtn(self, event):
dlg = wx.ProgressDialog('Working', 'Working', parent=self)
dlg.Pulse()
t = threading.Thread(None, SQLWork)
t.start()
while t.is_alive():
t0 = time.time()
wx.Yield()
print('# Yield', time.time()-t0)
'''
Every yielding takes about 1 ms.
However first 1-3 prints hang for about 10 seconds.
It prints like,
# Yield <---- At first time does not show up for a while
# Yield 0.34413034134 <---- It appears after few seconds, and its time is obviously different from actual wating time
# Yield 0.34413034134
# Yield 0.41312341955 <---- It happens 1-3 times
# Yield 0.34413034134
# Yield 0.41312341955
# Yield 0.00104315351
# Yield 0.00113210754
# Yield 0.00109634524 <---- Then it goes correct
'''
dlg.Destroy()
wx.Yield()
dlg = wx.MessageDialog(self, 'Work Done', 'Work Done')
dlg.ShowModal()
dlg.Destroy()
if __name__ == '__main__':
app = wx.App()
MainFrame().Show()
app.MainLoop()