Add multithreading - performance testing inconclusive

This commit is contained in:
askiiart 2024-02-28 22:55:01 -06:00
parent 3eea7435ed
commit 6acf3b0f3e
Signed by untrusted user who does not match committer: askiiart
GPG key ID: BC3800E55FB54D67

View file

@ -2,6 +2,29 @@ import gpg
import hkp4py import hkp4py
import os import os
import sys import sys
import asyncio
import subprocess
async def get_pubkey(server, fingerprint, log):
# This isn't perfect, it's a loose search
# for example searching for 7C79FCBB8372E5DE5B17E09A90D4B9641E092971 on keyserver.ubuntu.com returns the wrong key. But it works 99% of the time.
#return hkp4py.KeyServer(f'hkp://{server}').search(f'0x{fingerprint}')
results = await subprocess.getstatusoutput(f'gpg --keyserver {server} --recv-key {fingerprint}')
log.write(f'{server}: {results}\n')
print(f'{server}: {results}')
return results
def get_first_real_result(results):
for s in results:
s = list(s)
for task in s:
try:
return task.result()
except asyncio.exceptions.InvalidStateError:
pass
return None
XDG_DATA_HOME = os.getenv('XDG_DATA_HOME') XDG_DATA_HOME = os.getenv('XDG_DATA_HOME')
HOME = os.path.expanduser('~') HOME = os.path.expanduser('~')
@ -14,7 +37,8 @@ email = ''.join(open(filename, 'rt').readlines())
if 'gpg-email-helper' not in os.listdir(f'{XDG_DATA_HOME}'): if 'gpg-email-helper' not in os.listdir(f'{XDG_DATA_HOME}'):
os.mkdir(f'{XDG_DATA_HOME}/gpg-email-helper') os.mkdir(f'{XDG_DATA_HOME}/gpg-email-helper')
with open(f'{XDG_DATA_HOME}/gpg-email-helper/log', 'a') as log:
async def main(log):
log.write(f'\n\nRunning on {filename}\n') log.write(f'\n\nRunning on {filename}\n')
if ( if (
@ -83,29 +107,23 @@ with open(f'{XDG_DATA_HOME}/gpg-email-helper/log', 'a') as log:
'key-server.org', 'key-server.org',
'pgp.id', 'pgp.id',
'sks.ygrek.org', 'sks.ygrek.org',
# 'pgp.mit.edu' # still in operation but very unreliable #'pgp.mit.edu' # still in operation but very unreliable
] ]
for server in keyservers: tasks = [
log.write(f'{server}: ') asyncio.create_task(get_pubkey(server, fingerprint, log))
try: for server in keyservers
pubkey = hkp4py.KeyServer( ]
# This isn't perfect, it's a loose search #tasks = set(tasks)
# for example searching for 7C79FCBB8372E5DE5B17E09A90D4B9641E092971 on keyserver.ubuntu.com returns the wrong key. But it works 99% of the time. results = await asyncio.gather(tasks)
f'hkp://{server}' print(1)
).search(f'0x{fingerprint}') print(get_first_real_result(results))
if pubkey != None: print(3)
import_result = gpg.Context().key_import( print(get_first_real_result(results))
pubkey[0].key.encode()
)
log.write(
f'Attempted to import from {server}: {import_result}\n'
)
if type(import_result) == gpg.results.ImportResult:
break
else:
log.write('None\n')
except Exception as e:
log.write(f'[ERROR] {e}\n\n')
except Exception as e: except Exception as e:
log.write(f'[ERROR] {e}\n\n') log.write(f'[ERROR] {e}\n\n')
with open(f'{XDG_DATA_HOME}/gpg-email-helper/log', 'a') as log:
asyncio.run(main(log))