Skip to content

Instantly share code, notes, and snippets.

@Avlyssna
Created March 5, 2018 23:07
Show Gist options
  • Save Avlyssna/17f232a953d1d7809351802c2efad653 to your computer and use it in GitHub Desktop.
Save Avlyssna/17f232a953d1d7809351802c2efad653 to your computer and use it in GitHub Desktop.
# Standard library imports
import threading
import queue
class ConcurrentFunction:
def __init__(self, function, args=(), kwargs={}):
self.function = function
self.args = args
self.kwargs = kwargs
def run(self, results_queue, *args, **kwargs):
result = self.function(*args, **kwargs)
results_queue.put(result)
def run_concurrently(concurrent_functions):
results_queue = queue.Queue()
results = []
for concurrent_function in concurrent_functions:
threading.Thread(
target=concurrent_function.run,
args=(results_queue, *concurrent_function.args),
kwargs=concurrent_function.kwargs).start()
while len(results) < len(concurrent_functions):
results.append(results_queue.get())
return results
'''
The purpose behind this is to take multiple calls to a time-consuming function (with different
inputs) and run them all at once without the need for custom implementation. We return a list of
similar results that can be dealt with in any way the caller wants. HOWEVER, there is no guarantee
that the order of the results will match the order of the input functions.
Usage:
```python
import concurrency
def get_echo(string='Ada Lovelace'):
return string
results = concurrency.run_concurrently([
concurrency.ConcurrentFunction(get_echo),
concurrency.ConcurrentFunction(get_echo, ['Ada']),
concurrency.ConcurrentFunction(get_echo, kwargs={'string': 'Lovelace'})
])
print(results)
# ['Ada Lovelace', 'Ada', 'Lovelace']
```
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment