Skip to content

Instantly share code, notes, and snippets.

@dalf
Last active January 12, 2023 16:52
Show Gist options
  • Save dalf/4863421303d921f63342a696f21ee068 to your computer and use it in GitHub Desktop.
Save dalf/4863421303d921f63342a696f21ee068 to your computer and use it in GitHub Desktop.
from concurrent.futures import ThreadPoolExecutor
from time import time
import pathlib
import requests
SESSION = requests.Session()
def get_content(filename):
with open(filename, 'rb') as f:
return f.read()
output_count = 0
def send_request(content):
global output_count
try:
response = SESSION.post('http://localhost:8888/ocr/?max_time=7', data=content, headers={'Content-Type': 'application/x-www-form-urlencoded'})
except Exception as e:
print('Exception', e)
output_count += 1
result = response.json()
try:
print(result)
if result.get('success'):
print(output_count)
with open('zz/output_' + str(output_count) + '.json', 'wb') as f:
f.write(response.content)
else:
print('error', response.text)
pathlib.Path('zz/output_' + str(output_count) + '_false.json').touch()
except Exception as e:
print(e)
def main():
l = list(pathlib.Path('images').glob('*.*'))
# l = ['test.jpg', 'test.png', 'test2.jpg', 'test3.jpg', 'test4.jpg']
l = [get_content(f) for f in l]
count = 0
with ThreadPoolExecutor(max_workers=4) as pool:
time_before = time()
for _ in range(0, 4):
for content in l:
pool.submit(send_request, content)
count += 1
runtime = time() - time_before
print('%s requests, %s second/request, %s seconds in total' % (count, round(runtime / count, 3), runtime))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment