If startChangeStream and stopChangeStream are different scripts (or even different functions within the same script), then I don’t think this approach will work, since they are basically two different change streams, so one cannot affect the other.
Also in the stopChangeStream script/function:
with collection.watch() as stream:
for change in stream:
break
wouldn’t this open a changestream and immediately close it?
I believe what you need is to combine them into one function, something like what I posted earlier:
with collection.watch() as stream:
print(change)
if <some external condition>:
break
The <some external condition> above could be anything you need, e.g. a flag that was set somewhere, some timer, etc.
Best regards
Kevin