-
Notifications
You must be signed in to change notification settings - Fork 361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to on_next from background thread in main thread? #667
Comments
Hi @alek5k
Below repro is actually not relevant as all examples that show "MainThread" are actually calling thread_to_execute_on = CurrentThreadScheduler()
def print_thread(prefix: str):
return lambda *x: print(prefix, threading.current_thread().name, x)
of(1, 2, 3).pipe(ops.observe_on(thread_to_execute_on)).subscribe(print_thread("OF"))
# => OF MainThread (1,) ; OF MainThread (2,) ; OF MainThread (3,)
timer(2).subscribe(on_next=print_thread("TIMER"), scheduler=thread_to_execute_on)
# => Do not schedule blocking work! <- warning ; TIMER MainThread (0,)
my_subject = Subject()
my_subject.pipe(ops.observe_on(thread_to_execute_on)).subscribe(print_thread('SUBJECT'))
# => SUBJECT Thread-1 ('hello',)
|
Sorry my comment above was not a valid repro at all, apologies. This is actually stated in the doc and can be seen in the code:
and code where a new trampoline is created depending on the def get_trampoline(self) -> Trampoline:
thread = current_thread()
tramp = self._tramps.get(thread)
if tramp is None:
tramp = Trampoline()
self._tramps[thread] = tramp
return tramp Will investigate further how to achieve what you were asking for. |
Thanks @matiboy , actually I forgot that I have raised a similar issue in the past at this issue. I guess I didn't really get to a solution. The ability to do work on the main thread is pretty simple in C# RX and it would definitely be nice to have in RxPY. A lot of the responses seem to assume that you have a UI event loop, which is not always the case, for example, imagine a while loop in the main thread which waits on a user input(), like a CLI. |
Hi, I want to pass data from a background thread into the main thread. I have the following code, but it seems that
CurrentThreadScheduler()
is not doing what I expect.I'm using
reactivex
version 4.2.The output is:
I've also tried using
ThreadPoolScheduler
and the data is correctly passed to the threadpool thread. In that scenario, output is:Is there something I can use to schedule work back on the main thread? for example:
ops.observe_on(MainThreadScheduler())
. This seems to be quite simple to do in C# and java.To be clear, the output I am after is:
The text was updated successfully, but these errors were encountered: