我正在运行需要时间才能完成的功能。用户可以选择停止此功能/事件。有没有简单的方法来停止线程或循环?
class ThreadsGenerator:
MAX_WORKERS = 5
def __init__(self):
self._executor = ThreadPoolExecutor(max_workers=self.MAX_WORKERS)
self.loop = None
self.future = None
def execute_function(self, function_to_execute, *args):
self.loop = asyncio.get_event_loop()
self.future = self.loop.run_in_executor(self._executor, function_to_execute, *args)
return self.future
Run Code Online (Sandbox Code Playgroud)
我想在用户单击停止按钮时尽快停止该功能,而不是等待完成其工作。
提前致谢!
python multithreading python-3.x python-asyncio concurrent.futures
list = []和之间有什么区别list.clear()?
根据代码的行为和我自己的观察,list.clear()删除其条目以及用于添加其数据的条目。
例:
container.append(list)
list.clear()
Run Code Online (Sandbox Code Playgroud)
container 也将是 []