Nir Soffer has uploaded a new change for review.
Change subject: concurrent: Add support for target function kwargs ......................................................................
concurrent: Add support for target function kwargs
Turns out we pass kwargs to some threads, so we must support this syntax.
Change-Id: I5bcc24686279f9511baa83e64ac186533513cbd3 Signed-off-by: Nir Soffer nsoffer@redhat.com --- M lib/vdsm/concurrent.py M tests/concurrentTests.py 2 files changed, 19 insertions(+), 2 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/70/45470/1
diff --git a/lib/vdsm/concurrent.py b/lib/vdsm/concurrent.py index 5a9df85..6de35c7 100644 --- a/lib/vdsm/concurrent.py +++ b/lib/vdsm/concurrent.py @@ -151,7 +151,7 @@ return results
-def thread(func, args=(), name=None, daemon=True, logger=None): +def thread(func, args=(), kwargs=None, name=None, daemon=True, logger=None): """ Create a thread for runnning func with args.
@@ -161,6 +161,8 @@
args Arguments to pass to func
+ kwargs Keyword arguments to pass to func + name If set, set thread name.
daemon If True, create a daemon thread. @@ -168,9 +170,12 @@ logger If set, unhandled exception will be logged on this logger. Otherwise the root logger will be used. """ + if kwargs is None: + kwargs = {} + @utils.traceback(on=logger) def run(): - return func(*args) + return func(*args, **kwargs)
thread = threading.Thread(target=run, name=name) thread.daemon = daemon diff --git a/tests/concurrentTests.py b/tests/concurrentTests.py index 62ca833..d6c212a 100644 --- a/tests/concurrentTests.py +++ b/tests/concurrentTests.py @@ -229,3 +229,15 @@ t.start() t.join() self.assertEqual((1, 2, 3), self.args) + + def test_pass_kwargs(self): + self.kwargs = () + + def run(**kwargs): + self.kwargs = kwargs + + kwargs = {'a': 1, 'b': 2} + t = concurrent.thread(run, kwargs=kwargs) + t.start() + t.join() + self.assertEqual(kwargs, self.kwargs)