Created
October 12, 2022 17:37
-
-
Save ppaska/f9b7bf45c57515b5e4bed98fd6fc090e to your computer and use it in GitHub Desktop.
Make a Sequential vs. Parallel API Calls in JSPython
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
from 'rxjs' import forkJoin, lastValueFrom, map | |
ids = [2, 7, 4, 9, 5] | |
async def run_Sequential(): | |
data = [] | |
for requestId in ids: | |
response = httpGet("https://jsonplaceholder.typicode.com/posts/" + requestId) | |
data.push({requestId, response}) | |
return data | |
async def run_Parallel(): | |
httpRequests$ = ids | |
.map( | |
requestId => httpRequest$("GET", "https://jsonplaceholder.typicode.com/posts/" + requestId) | |
.pipe( | |
map(r => {requestId, response: r.data}) | |
) | |
) | |
return lastValueFrom(forkJoin(httpRequests$)) | |
if __env.entryFunction == '': | |
return { | |
sequential: run_Sequential(), | |
parallel: run_Parallel() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment