Created
November 11, 2018 21:27
-
-
Save amiller/b773755111bd64adc96cd1783cd44ed6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import concurrent.futures | |
import logging | |
import time | |
import random | |
# Examples of running in executor | |
# https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor | |
# https://pymotw.com/3/asyncio/executors.html | |
from pypairing import py_pairing, PyG1, PyG2, PyFq12 | |
def test_sequential(n=1000): | |
g1 = PyG1(0,0,0,1) | |
g2 = PyG2(0,0,0,1) | |
result = PyFq12() | |
start = time.time() | |
print(f'starting {n} pairings: {time.time()}') | |
for i in range(n): | |
g1.py_pairing_with(g2, result) | |
duration = time.time() - start | |
print(f'{duration}') | |
test_sequential(n=1000) | |
test_sequential(n=1000) | |
def slow_computation(n): | |
# Invoke a CFFI function that takes a while | |
log = logging.getLogger('slow_computation({})'.format(n)) | |
log.info('running') | |
test_sequential(1000) | |
log.info('done') | |
return n | |
async def test_threads(): | |
# Create a limited thread pool. | |
executor = concurrent.futures.ThreadPoolExecutor( | |
max_workers=2, | |
) | |
loop = asyncio.get_event_loop() | |
# Wrap a slow computation by calling it in the thread pool | |
def wrap_slow_computation(n): | |
return loop.run_in_executor(executor, slow_computation, n) | |
log = logging.getLogger('test_threads') | |
log.info('creating executor tasks') | |
tasks = [wrap_slow_computation(i) for i in range(7)] | |
log.info('waiting for executor tasks') | |
completed, pending = await asyncio.wait(tasks) | |
results = [t.result() for t in completed] | |
log.info('results: {!r}'.format(results)) | |
log.info('exiting') | |
if __name__ == '__main__': | |
# Configure logging to show the name of the thread | |
# where the log message originates. | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(threadName)10s %(name)18s: %(message)s' | |
) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(test_threads()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment