When I submit to gspread it takes some time for data to get uploaded and this hangs my program for a few seconds. I was hoping to just have that submital appended to a local list and in the background have that list uploaded as to not bog down the UI. I am very new to python and while everything works fine (code is abridged below for brevity) I am unable to consistently upload the data without a "main thread not in main loop" exception. I searched for the answer here and at other sites but documentation and help on the queue to thread implementation is few and far between. Any help would be appreciated. Thank you.
#!/usr/bin/python
import sys
from tkinter import *
from tkinter.ttk import *
import gspread
import threading
import queue
#Main window
root= Tk()
#global variables
editssincelastupload={}
newsincelastupload={}
WorkbookGS=gspreadworkbook()
last_row_no=lastrowfromgspread()
submital_queue = queue.Queue(maxsize=0)
#quit function but make sure all things are sumbited first
def mQuit():
worker.join()
root.destroy()
retu
#want to continually check the queue and submit it in the background thread
def submitloadedoffline(i,q):
while True:
submital = q.get()
if submital in newsincelastupload:
submitnewtransonline(submital)
if submital in editssincelastupload:
submitedittransonline(submital)
q.task_done()
#submits edits to transaction
def submitedittransonline(submital):
global editssincelastupload
addtospreadsheet(submital, editssincelastupload[submital][0])
editssincelastupload=removekey(editssincelastupload,submital)
retu
#submits new transactions
def submitnewtransonline(submital):
global newsincelastupload
addtospreadsheet(submital, newsincelastupload[submital][0])
newsincelastupload=removekey(newsincelastupload,submital)
retu
#just removes key from dict
def removekey(d, key):
if dict(d)=={}:
retu d
r = dict(d)
del r[key]
retu r
#adds data to the lists
def formsubmitCallBack():
global last_row_no, editssincelastupload, newsincelastupload
rownumber=someintfromform
value_list=[values]
submital_list=[values2]
if rownumber!=int(last_row_no)+1:
editssincelastupload[rownumber]=[value_list,submital_list]
else:
newsincelastupload[rownumber]=[value_list,submital_list]
retu
#submits data to gspread
def addtospreadsheet(rownumber, value_list):
global WorkbookGS,last_row_no
if int(rownumber)==(int(last_row_no)+1):
WorkbookGS.append_row(value_list)
retu
else:
startcell='A'+str(rownumber)
endcell='AK'+str(rownumber)
cellrange=str(startcell+':'+endcell)
cell_list=WorkbookGS.range(cellrange)
for i,val in enumerate(value_list):
cell_list[i].value = val
WorkbookGS.update_cells(cell_list)
#button to add data to lists
b=Button(root,command=formsubmitCallBack)
b.grid()
# Set up some threads to submit from queue
for i in range(len(newsincelastupload)+len(editssincelastupload)):
worker = threading.Thread(target=submitloadedoffline, args=(i, submital_queue))
worker.setDaemon(True)
worker.start()
#put things in queue
for submital in newsincelastupload:
print( 'Queuing:', submital)
submital_queue.put(submital)
for submital in editssincelastupload:
print( 'Queuing:', submital)
submital_queue.put(submital)
#makes sure all quits are via mQuit so threads are not stopped prematurely
root.protocol("WM_DELETE_WINDOW", mQuit)
root.mainloop()
