would bad practice, or there better way such thing? have program has cli menu system take user input , based off input set query database data, , analysis on it.
what i'd do, however, set query datetime start, add queue. optimally, put on event loop menu system, menu system pauses while waiting user input (either through built in python input function or through curses window.getkey function). in addition, want still able use menu while data being analysed
so i'm doing using 2 threads, 1 check if there's objects in queue datetime of less datetime.now(), remove queue, analysis, , continue checking queue.
class analysisqueue(threading.thread): def __init__(self, initial_queue): super(analysisqueue, self).__init__() self.alive = true self.queue = initial_queue def run(self): while self.alive: #loop can terminated externally i,object in enumerate(self.queue): if datetime.now() > object.analysis_start: analyse_data(self.queue.pop(i)) #defined elsewhere class menu(threading.thread): def __init__(self): super(menu, self).__init__() self.date_menu = multiplechoicemenu([ {'description': "test1"}, {'description': "test2"}, {'description': "test3"}, ]) #menu class handles display , i/o menu def run(self): self.date_menu.input() #initializes menu display , waits input analysis_thread = analysisqueue() menu_thread = menu() analysis_thread.start() menu_thread.start() menu_thread.join() #waits menu thread finish (menu exited) analysis_thread.alive = false #now menu has been exited, terminate program. #whether or not queue has entries in @ point not concern is there wrong approach, or rather, there better way it?
thanks help!
this quite open-ended question, more code review, here's feedback:
- you use priority queue instead of going through whole list looking timestamps. need @ head of queue.
- i'd change
self.aliveboolevent. thread-safe. - this looks regular work queue me, used keep gui thread responsive. don't understand reason timestamps - why not run tasks immediately?
- how communicate result of analysis main thread?
- your code appending tasks queue not included. if doing
analysis_thread.queue.append(task)trouble - since implementation using list. lists not thread-safe (maybe appends atomic, i'm not surepopis), you'll need synchronization that. - maybe can try separate logic of queueing , worker thread.
in conclusion. i'd set worker, did, , regular queue tasks. maybe this start?
Comments
Post a Comment