From b65c79c0132d7bb5a9b22fe98ad884aa307919a2 Mon Sep 17 00:00:00 2001 From: Cedric Veilleux Date: Fri, 24 Mar 2023 14:06:59 -0400 Subject: [PATCH 1/2] perf: Use a set instead of a list to keep track of in-flight seed ids. The `in` check is much faster on a set which is designed to contain unique items. --- pythonping/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 2b3c29e..1a75b64 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -5,7 +5,7 @@ # this needs to be available across all thread usages and will hold ints -SEED_IDs = [] +SEED_IDs = set() def ping(target, @@ -74,7 +74,7 @@ def ping(target, # seed_id needs to be less than or equal to 65535 (as original code was seed_id = getpid() & 0xFFFF) seed_id = randint(0x1, 0xFFFF) if seed_id not in SEED_IDs: - SEED_IDs.append(seed_id) + SEED_IDs.add(seed_id) break From fe9eb1362b2d94318712506aff9926af6602bcbb Mon Sep 17 00:00:00 2001 From: Cedric Veilleux Date: Fri, 24 Mar 2023 14:08:25 -0400 Subject: [PATCH 2/2] fix: Clean-up seed_id from in-flight set of SEED_IDs using a try/finally block. --- pythonping/__init__.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pythonping/__init__.py b/pythonping/__init__.py index 1a75b64..bb52fea 100644 --- a/pythonping/__init__.py +++ b/pythonping/__init__.py @@ -77,12 +77,13 @@ def ping(target, SEED_IDs.add(seed_id) break + try: + comm = executor.Communicator(target, provider, timeout, interval, socket_options=options, verbose=verbose, output=out, + seed_id=seed_id, source=source, repr_format=out_format) - comm = executor.Communicator(target, provider, timeout, interval, socket_options=options, verbose=verbose, output=out, - seed_id=seed_id, source=source, repr_format=out_format) + comm.run(match_payloads=match) - comm.run(match_payloads=match) - - SEED_IDs.remove(seed_id) + finally: + SEED_IDs.remove(seed_id) return comm.responses