Python线程和PySimpleGUI
--使用MikeyB的解决方案进行了修订-- 感谢Mikey指出了一个简单的解决方案。我觉得有时候在一个解决方案中投入了太多的精力,这是一个简单的开销解决方案,可以解决这个问题 我添加了一个小函数,该函数在我希望监视的目录中循环,并将变量设置为True或FalsePython线程和PySimpleGUI,python,multithreading,function,queue,pysimplegui,Python,Multithreading,Function,Queue,Pysimplegui,--使用MikeyB的解决方案进行了修订-- 感谢Mikey指出了一个简单的解决方案。我觉得有时候在一个解决方案中投入了太多的精力,这是一个简单的开销解决方案,可以解决这个问题 我添加了一个小函数,该函数在我希望监视的目录中循环,并将变量设置为True或False def file_check(working_pdf): if len(gb1(working_pdf, '*.pdf')) == 0: pdf_available = False if len(gb
def file_check(working_pdf):
if len(gb1(working_pdf, '*.pdf')) == 0:
pdf_available = False
if len(gb1(working_pdf, '*.pdf')) > 0:
pdf_available = True
return pdf_available
然后在PySimpleGUI事件循环中调用该函数
if files_available is True:
for client in client_running_list:
working_pdf, ext = folder_Func(client)
pdf_available = file_check(working_pdf)
if pdf_available is True:
analyzer_queue.put(client)
for x in range(10):
t = Thread(target=analyzer)
t.daemon = True
t.start()
--原职--
我有一个程序,它在通过函数定义的目录中查找,如果有文件,它会解析这些文件,然后将数据移动到数据库中。如果程序启动时目录中有文件,它将按预期运行,但当添加新文件时,该函数不会执行。似乎无限循环没有通过目录执行
我有一个通过PySimpleGUI的UI,它使用一个“while True:”循环,所以我必须通过一个线程派生函数。我使用的是一个队列,我试图确定在哪里需要一个“while True:”循环,以便在文件夹中不断查找新文件
以下是代码的一部分(以下缩进不合适):
您可以在代码中的PySimpleGUI事件循环中寻找新事物、轮询硬件、执行任何不需要很长时间的“检查” 通过向读取调用添加超时,事件循环将定期运行。使用此选项定期检查新文件。这也是可用于使用队列检查来自线程的传入消息的相同技术
为True时:#事件循环
事件,值=window.read(超时=500)#每500毫秒返回一次
如果事件处于(无,'退出'):
打破
如果检查\u是否有更改():
做某事
你每一秒都在跑步。这对于轮询新文件应该很好。将while循环添加到事件循环中。如果太长,请按照您所说的将其旋转为一个线程,并将check_for_更改替换为check_for_message_from_threads.好了!很好地解决了这个问题。您可以在事件循环中做很多方便的事情,包括启动执行长任务的线程,检查线程设置的全局变量,检查队列或轮询硬件或。一旦您编写了更多PySimpleGUI代码,请查看更新的编码约定。你会喜欢紧凑的。您可以缩短为dashboard_form[f'Stop Analyze'].Update(disabled=True)之类的内容。不需要打电话给FindElement。哦,再给你一个提示。。您可以在创建线程时设置daemon=True。这是线程的一个参数。谢谢你的提示。新的编码约定在哪里?PySimpleGUI的github?是的,文档中的示例和演示程序都更新为使用最新的编码约定
def analyzer():
while True:
client = analyzer_queue.get()
working_pdf, archive_path_datetime = folder_Func(client)
while True:
if len(gb1(working_pdf, '*.pdf')) == 0:
break
else:
print(f'Found files in ', client, ' folder. Starting Parse.')
##########################################################
# Start Parse of PDF's
# Calls pdf parse function.
# Arguments are Client Number, and PDF to parse.
# Returns DF of items to insert into SQL Database
##########################################################
ch(working_pdf)
for pdf in gb1(working_pdf, "*.pdf"):
items_found_df = pdf_parse(client, pdf)
##########################################################
# Connect to SQL Server and insert items
# Calls database connection function.
##########################################################
azureDBengine = sqlalchemyConn()
items_found_df.to_sql("MainData_Capture",azureDBengine,if_exists='append',method='multi',index=False)
##########################################################
# Move file to Archive
##########################################################
if not ospath.exists(archive_path_datetime):
osmakedirs(archive_path_datetime)
print("Created Archive Folder.")
file_move(working_pdf, archive_path_datetime, pdf)
print('All Files Processed.')
analyzer_queue.task_done()
while True:
event_dashboard, values_dashboard = dashboard_form.Read(timeout=1000)
if dashboard_form is None or event_dashboard == 'Exit':
dashboard_form.Close()
break
for client in active_client_list:
client_start_button_action(client, event_dashboard, dashboard_form)
client_stop_button_action(client, event_dashboard, dashboard_form)
if event_dashboard == 'Start Analyze':
dashboard_form.FindElement(f'Start Analyze').Update(disabled=True)
dashboard_form.FindElement(f'Stop Analyze').Update(disabled=False)
print('Analyzer Started')
for client in client_running_list:
analyzer_queue.put(client)
for x in range(10):
t = Thread(target=analyzer)
t.daemon = True
t.start()
if event_dashboard == 'Stop Analyze':
dashboard_form.FindElement(f'Stop Analyze').Update(disabled=True)
dashboard_form.FindElement(f'Start Analyze').Update(disabled=False)
print('Analyzer Stopped')
analyzer_queue.empty()