def func2(n): if n > 10: # This exception is treated in func1 raise Exception(10) launches = [] for i in range(n): launches.append(futures.submit(func3, i + 1)) result = futures.as_completed(launches) return sum(res.result() for res in result)
def main(): # Create launches launches = [futures.submit(myFunc, i + 1) for i in range(5)] # Add a callback on every launches for launch in launches: launch.add_done_callback(doneElement) # Wait for the launches to complete. [completed for completed in futures.as_completed(launches)]
def run(values, parameters): # Create launches launches = [futures.submit(func, values, parameters, i) for i in cycle] # Add a callback on every launches for launch in launches: launch.add_done_callback(doneElement) # Wait for the launches to complete. [completed for completed in futures.as_completed(launches)]
def func2(n): launches = [futures.submit(func3, i + 1) for i in range(n)] # Spawn a generator for each completion, unordered result = (a.result() for a in futures.as_completed(launches)) return sum(result)
def func2(n): launches = [] for i in range(n): launches.append(futures.submit(func3, i + 1)) result = futures.as_completed(launches) return sum(r.result() for r in result)
import time def getSize(string): """ This functions opens a web sites and then calculate the total size of the page in bytes. This is for the sake of the example. Do not use this technique in real code as it is not a very bright way to do this.""" try: # We open the web page with urllib.request.urlopen(string, None, 1) as f: return sum(len(line) for line in f) except (urllib.error.URLError, socket.timeout) as e: return 0 if __name__ == "__main__": # The pageurl variable contains a link to a list of web sites. It is # commented for security's sake. pageurl = "http://httparchive.org/lists/Fortune%20500.txt" #pageurl = "http://www.example.com" with urllib.request.urlopen(pageurl) as pagelist: pages = [page.decode() for page in pagelist][:30] # This will apply the getSize function on every item of the pages list # in parallel. The results will be treated as they are produced. fut = [futures.submit(getSize, page) for page in pages] for f in futures.as_completed(fut): time.sleep(0.1) # Work on the data print(f.result())
from scoop import futures import socket import time def getSize(string): """ This functions opens a web sites and then calculate the total size of the page in bytes. This is for the sake of the example. Do not use this technique in real code as it is not a very bright way to do this.""" try: # We open the web page with urllib.request.urlopen(string, None, 1) as f: return sum(len(line) for line in f) except (urllib.error.URLError, socket.timeout) as e: return 0 if __name__ == "__main__": # The pageurl variable contains a link to a list of web sites. It is # commented for security's sake. pageurl = "http://httparchive.org/lists/Fortune%20500.txt" #pageurl = "http://www.example.com" with urllib.request.urlopen(pageurl) as pagelist: pages = [page.decode() for page in pagelist][:30] # This will apply the getSize function on every item of the pages list # in parallel. The results will be treated as they are produced. fut = [futures.submit(getSize, page) for page in pages] for f in futures.as_completed(fut): time.sleep(0.1) # Work on the data print(f.result())