Add multithreading - performance testing inconclusive
This commit is contained in:
parent
3eea7435ed
commit
6acf3b0f3e
1 changed files with 41 additions and 23 deletions
|
@ -2,6 +2,29 @@ import gpg
|
|||
import hkp4py
|
||||
import os
|
||||
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')
|
||||
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}'):
|
||||
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')
|
||||
|
||||
if (
|
||||
|
@ -83,29 +107,23 @@ with open(f'{XDG_DATA_HOME}/gpg-email-helper/log', 'a') as log:
|
|||
'key-server.org',
|
||||
'pgp.id',
|
||||
'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:
|
||||
log.write(f'{server}: ')
|
||||
try:
|
||||
pubkey = hkp4py.KeyServer(
|
||||
# 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.
|
||||
f'hkp://{server}'
|
||||
).search(f'0x{fingerprint}')
|
||||
if pubkey != None:
|
||||
import_result = gpg.Context().key_import(
|
||||
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')
|
||||
tasks = [
|
||||
asyncio.create_task(get_pubkey(server, fingerprint, log))
|
||||
for server in keyservers
|
||||
]
|
||||
#tasks = set(tasks)
|
||||
results = await asyncio.gather(tasks)
|
||||
print(1)
|
||||
print(get_first_real_result(results))
|
||||
print(3)
|
||||
print(get_first_real_result(results))
|
||||
|
||||
except Exception as e:
|
||||
log.write(f'[ERROR] {e}\n\n')
|
||||
|
||||
|
||||
with open(f'{XDG_DATA_HOME}/gpg-email-helper/log', 'a') as log:
|
||||
asyncio.run(main(log))
|
||||
|
|
Loading…
Reference in a new issue