refactor: don't spin up a thread pool for a single notification (#846)

pull/847/head
Ryan Young 2023-03-01 04:34:18 -08:00 committed by GitHub
parent 3a2af45e4d
commit 2057107590
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -583,10 +583,16 @@ class Apprise:
Process a list of notify() calls in parallel and synchronously. Process a list of notify() calls in parallel and synchronously.
""" """
n_calls = len(servers_kwargs)
# 0-length case # 0-length case
if not servers_kwargs: if n_calls == 0:
return True 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 # Create log entry
logger.info( logger.info(
'Notifying %d service(s) with threads.', len(servers_kwargs)) '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. Process a list of async_notify() calls in parallel and asynchronously.
""" """
n_calls = len(servers_kwargs)
# 0-length case # 0-length case
if not servers_kwargs: if n_calls == 0:
return True 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 # Create log entry
logger.info( logger.info(
'Notifying %d service(s) asynchronously.', len(servers_kwargs)) 'Notifying %d service(s) asynchronously.', len(servers_kwargs))

View File

@ -1866,8 +1866,8 @@ def test_apprise_async_mode(mock_threadpool, mock_gather, mock_post, tmpdir):
# Send 1 Notification Syncronously, the other Asyncronously # Send 1 Notification Syncronously, the other Asyncronously
assert a.notify("a mixed batch") is True assert a.notify("a mixed batch") is True
# Verify our thread pool was created # Verify we didn't use a thread pool for a single notification
assert mock_threadpool.call_count == 1 assert mock_threadpool.call_count == 0
mock_threadpool.reset_mock() mock_threadpool.reset_mock()