This is relevant if I have a list of tasks I want to queue, but I don’t know how long it will take, and I don’t want to interrupt any task whilst its ongoing.
I like to queue my tasks without having to be too careful. Currently I sometimes have to deal with corrupted files because I wasn’t careful enough. (I queue them up in a for loop and use KeyBoardInterrupt to abort.)
I can probably bodge something together using Threading, but if something already exists it’s fairly likely to be of better quality.
I’m not familiar enough with the queue or threading packages to recognise your code snippet.
I guess any actual work to be done on items in the queue will have to go where the print statements are.
How do you add tasks to the queue? When does the code start running tasks?
Yes. The current task will finish, but all tasks after will be skipped
I would simply add a condition to safely break the loop once the task has finished:
condition = True
tasks = glob_query()
flag_file = Path("flag.txt")
flag_file.touch()
for task in tasks:
if flag_file.exists():
do()
some()
stuff()
to(task)
if not condition: break
import signal
import time
# Initial condition
condition = True
# Function to handle the signal and change the condition
def signal_handler(sig, frame):
global condition
condition = False
print("\nAborted! Setting condition to False.")
# Set up signal handling
signal.signal(signal.SIGINT, signal_handler)
tasks = [1, 2, 3, 4]
for task in tasks:
print('START')
print(task)
time.sleep(4.0)
print('END')
if not condition:
print('Task finished!')
break