Commit b94caeb1 authored by Nane Kratzke's avatar Nane Kratzke
Browse files

parallel space

parent 149d9567
...@@ -5,7 +5,6 @@ import requests ...@@ -5,7 +5,6 @@ import requests
import traceback import traceback
import os import os
import json import json
from joblib import Parallel, delayed from joblib import Parallel, delayed
# A very boring and basic space-service to demonstrate # A very boring and basic space-service to demonstrate
...@@ -26,9 +25,11 @@ tracer = config.initialize_tracer() ...@@ -26,9 +25,11 @@ tracer = config.initialize_tracer()
def index(): def index():
return redirect('/space') return redirect('/space')
def load_url(url, key, ctx): def load_url(key, url):
with tracer.start_span(operation_name=key, child_of=ctx) as span: return key, json.loads(requests.get(url).content)
return key, json.loads(requests.get(url).content)
#with tracer.start_span(operation_name=key, child_of=ctx) as span:
# return key, json.loads(requests.get(url).content)
@app.route('/space') @app.route('/space')
def space(): def space():
...@@ -45,11 +46,11 @@ def space(): ...@@ -45,11 +46,11 @@ def space():
"position": "http://api.open-notify.org/iss-now.json", "position": "http://api.open-notify.org/iss-now.json",
"passtime": f"http://api.open-notify.org/iss-pass.json?lat={ lat }&lon={ lon }" "passtime": f"http://api.open-notify.org/iss-pass.json?lat={ lat }&lon={ lon }"
} }
#result = {}
results = Parallel(n_jobs=3)(delayed(load_url)(url, key, parent_span) for key, url in queries.items()) with tracer.start_span(operation_name="parallel", child_of=parent_span) as span:
result = { key: content for key, content in results } results = Parallel()(delayed(load_url)(key, url) for key, url in queries.items())
result = { key: content for key, content in results }
#for key, url in queries.items(): #for key, url in queries.items():
# with tracer.start_span(operation_name=key, child_of=parent_span) as span: # with tracer.start_span(operation_name=key, child_of=parent_span) as span:
# result[key] = json.loads(requests.get(url).content) # result[key] = json.loads(requests.get(url).content)
...@@ -57,4 +58,4 @@ def space(): ...@@ -57,4 +58,4 @@ def space():
print(result) print(result)
return jsonify(result) return jsonify(result)
app.run(host="0.0.0.0", port=80) app.run(host="0.0.0.0", port=80)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment