这会是一种不好的做法,还是有另一种更好的方法来做这样的事情?我目前所拥有的是一个程序,它具有一个CLI菜单系统来获取用户输入,并根据该输入设置查询数据库中的数据,并对其进行分析。
然而,我想要做的是设置查询时使用日期时间启动它,然后将其添加到队列中。最好的情况是,我会把它放在我的菜单系统的事件循环中,但菜单系统在等待用户输入时暂停(通过内置的python input
函数或通过curses window.getkey
函数)。此外,我希望在分析数据时仍能使用菜单
所以我正在做的是使用两个线程,一个用于检查队列中是否有任何日期时间小于datetime.now()
的对象,将其从队列中删除,进行分析,然后继续检查队列。
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 be terminated externally
for 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 that handles display and I/O for the menu
def run(self):
self.date_menu.input() #initializes the menu display and waits for input
analysis_thread = AnalysisQueue()
menu_thread = Menu()
analysis_thread.start()
menu_thread.start()
menu_thread.join() #waits for the menu thread to finish (menu is exited)
analysis_thread.alive = False
#Now that menu has been exited, terminate program.
#whether or not the queue has entries in it at this point is not a concern
这种方法有什么问题,或者更确切地说,有没有更好的方法呢?
感谢您的帮助!
答案 0 :(得分:2)
这是一个非常开放的问题,更像是代码审核,但这是我的反馈:
self.alive
从bool
更改为Event
。它们是线程安全的。analysis_thread.queue.append(task)
,那么您将遇到麻烦 - 因为您的实施是使用列表。列表不是线程安全的(也许附加是原子的,但我不确定pop
是),所以你可能需要一些同步。