Reputation: 35
--REVISED WITH SOLUTION FROM MikeyB--
Thanks to Mikey for pointing out a simple solution. I feel sometimes too much thought is put into a solution, and its a simple overhead solution that resolves the issue.
I have added in a small function that loops through my directories that I want monitored, and sets a variable to True or False.
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
This is then called in the PySimpleGUI event loop
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()
--Original Post--
I have a program that looks in directories defined through a function, and if there are files, it parses these files, and then it moves the data to a database. If there are files in the directories when the program starts it runs as intended, but when a new file is added the function does not execute. It seems that the infinite loop is not executing through directory.
I have a UI through PySimpleGUI that is using a "while True:" loop, so I have to spin off the function through a thread. I am using a queue, and I am trying to identify where I need a "while True:" loop to continuously look in the folder for new files.
Below is a portion of the code(indentations below are not proper):
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()
Upvotes: 2
Views: 8133
Reputation: 5764
You can look for new things, poll hardware, do any king of "checking" that doesn't take a long time in the PySimpleGUI event loop in your code.
By adding a timeout to your read call your event loop will run periodically. Use this to periodically check for your new files. This is also the same technique that can be used to check for incoming messages from threads using a Queue.
while True: # Event Loop
event, values = window.read(timeout=500) # returns every 500 ms
if event in (None, 'Exit'):
break
if check_for_changes():
do_something()
You're running yours every second. This should be fine for polling for new files. Add your while loop into your event loop. If it's too long, spin out as a thread as you said and replace the check_for_changes with check_for_message_from_threads.
Upvotes: 2