From 20571075904ad0d60124f173a5e235f8d98ba583 Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Wed, 1 Mar 2023 04:34:18 -0800 Subject: [PATCH] refactor: don't spin up a thread pool for a single notification (#846) --- apprise/Apprise.py | 16 ++++++++++++++-- test/test_api.py | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/apprise/Apprise.py b/apprise/Apprise.py index bb83a49e..7d37df61 100644 --- a/apprise/Apprise.py +++ b/apprise/Apprise.py @@ -583,10 +583,16 @@ class Apprise: Process a list of notify() calls in parallel and synchronously. """ + n_calls = len(servers_kwargs) + # 0-length case - if not servers_kwargs: + if n_calls == 0: return True + # There's no need to use a thread pool for just a single notification + if n_calls == 1: + return Apprise._notify_sequential(servers_kwargs[0]) + # Create log entry logger.info( 'Notifying %d service(s) with threads.', len(servers_kwargs)) @@ -619,10 +625,16 @@ class Apprise: Process a list of async_notify() calls in parallel and asynchronously. """ + n_calls = len(servers_kwargs) + # 0-length case - if not servers_kwargs: + if n_calls == 0: return True + # (Unlike with the thread pool, we don't optimize for the single- + # notification case because asyncio can do useful work while waiting + # for that thread to complete) + # Create log entry logger.info( 'Notifying %d service(s) asynchronously.', len(servers_kwargs)) diff --git a/test/test_api.py b/test/test_api.py index 4ea28365..a9848b18 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -1866,8 +1866,8 @@ def test_apprise_async_mode(mock_threadpool, mock_gather, mock_post, tmpdir): # Send 1 Notification Syncronously, the other Asyncronously assert a.notify("a mixed batch") is True - # Verify our thread pool was created - assert mock_threadpool.call_count == 1 + # Verify we didn't use a thread pool for a single notification + assert mock_threadpool.call_count == 0 mock_threadpool.reset_mock()