如何在rxpy中等待对主题的on_next()操作?

时间:2019-04-16 04:20:27

标签: python reactive-programming

我有一个主题subj = Subject(),其主题on_next(event)将通过不同的线程被异步调用

observable.subscribe(lambda event: subj.on_next(event), scheduler=NewThreadScheduler())

现在我想听听on_next()的操作,因此我将知道有一个新事件添加到subj中,我应该怎么做?

我希望类似

observable_stream = rx.from_(grpc_stream)
subj = Subject()

observable_stream.subscribe(lambda event: subj.on_next(event), scheduler=NewThreadScheduler()) 
// this will be ran in a different thread I assume? the observable_stream is from grpc and will receive event data continously

await subj.pipe(op.take(1))  
// I want to wait until the observable_stream has any new event being "on_next(event)" to subj, in the meanwhile subj.on_next(event) will be executed the thread on the previous line...

other functions to be executed...

因此,我可以等待将第一个事件添加到subj中,直到执行依赖于第一个事件的创建的其他任务。

我在上面尝试过的代码实际上并没有起作用,因为我在await subj.pipe(op.take(1))被阻止了,我很确定observable_stream收到了事件,并且on_next(event)的执行非常完美NewThreadScheduler,所以我执行代码时不会出现障碍。但是,行await subj.pipe(op.take(1))似乎永远等待着,永远都无法解决。我在哪里做错了?我应该使用其他调度程序还是其他有用的方法?

0 个答案:

没有答案