Skip to content

Instantly share code, notes, and snippets.

@dewaldabrie
Last active June 10, 2021 02:49
Show Gist options
  • Save dewaldabrie/021503a39be52d483a54e9f2e2d4155e to your computer and use it in GitHub Desktop.
Save dewaldabrie/021503a39be52d483a54e9f2e2d4155e to your computer and use it in GitHub Desktop.
Synchronous Generator Pipeline
import time
def func(targets, task_id, param=None):
print(f"{task_id}: Initialised with param: {param}")
while True:
inpt = (yield)
print(f"{task_id}: Received input: {inpt}")
time.sleep(1) # simulated IO delay
for target in targets:
print(f"{task_id}: T1 sending {inpt}")
target.send(inpt)
gen2 = func([], 'T2', "hello T2")
gen2.send(None)
gen1 = func([gen2], 'T1', "hello T1")
gen1.send(None)
start_time = time.time()
gen1.send(1)
gen1.send(2)
gen1.send(3)
print(f"Duration: {time.time() - start_time}")
@reservoirinvest
Copy link

Line #16 should be:
gen1 = func([gen2], 'T1', "hello T1")
instead of func[coro2] for the code to work.

@dewaldabrie
Copy link
Author

Line #16 should be:
gen1 = func([gen2], 'T1', "hello T1")
instead of func[coro2] for the code to work.

Thanks, I updated accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment