From 84d12e203c9d27bdf10961c044708c5e05dc512a Mon Sep 17 00:00:00 2001 From: Erik Welch Date: Sun, 29 Aug 2021 09:36:40 -0500 Subject: [PATCH] Allow `Output.clear_output()` to be thread-safe. Fixes #3260. --- ipywidgets/widgets/widget_output.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ipywidgets/widgets/widget_output.py b/ipywidgets/widgets/widget_output.py index 6d1bfdae4b..b28efe7c61 100644 --- a/ipywidgets/widgets/widget_output.py +++ b/ipywidgets/widgets/widget_output.py @@ -72,9 +72,16 @@ def clear_output(self, *pargs, **kwargs): wait: bool If True, wait to clear the output until new output is available to replace it. Default: False + + If any parameters are given, then this should be run on the main thread, + because race conditions may occur if run from multiple threads. It is + safe to call this function without any arguments from multiple threads. """ - with self: - clear_output(*pargs, **kwargs) + if not pargs and not kwargs: + self.outputs = () + else: + with self: + clear_output(*pargs, **kwargs) # PY3: Force passing clear_output and clear_kwargs as kwargs def capture(self, clear_output=False, *clear_args, **clear_kwargs):