Skip to content
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

Switching scheduler to Main Thread #655

Open
alek5k opened this issue Jul 7, 2022 · 2 comments
Open

Switching scheduler to Main Thread #655

alek5k opened this issue Jul 7, 2022 · 2 comments
Labels

Comments

@alek5k
Copy link

alek5k commented Jul 7, 2022

Hi there, just wondering how I can schedule work on specific threads, specifically the program's main thread? For example, I want to do certain map operations (here I use do_action) on separate threads, then use on_next the main thread. My reference to CurrentThreadScheduler seems to change once the ThreadPoolSchedulers are used - is this intended behaviour?

Here is some example code:

    import reactivex as rx
    from reactivex import operators as ops
    from reactivex.scheduler import ThreadPoolScheduler, CurrentThreadScheduler
    from threading import current_thread

    print(f"starting thread: {current_thread().name}")

    current_thread_scheduler = CurrentThreadScheduler()
    threadpool1 = ThreadPoolScheduler(1)
    threadpool2 = ThreadPoolScheduler(1)

    rx.just(1).pipe(
        ops.observe_on(threadpool1),
        ops.do_action(lambda _: print(f"step 1: {current_thread().name}")),
        ops.observe_on(threadpool2),
        ops.do_action(lambda _: print(f"step 2: {current_thread().name}")),
        ops.observe_on(current_thread_scheduler),
        ops.do_action(lambda _: print(f"step 3: {current_thread().name}")),
    ).subscribe(on_next=lambda _: print(f"on_next: {current_thread().name}"))

Output:

starting thread: MainThread
step 1: ThreadPoolExecutor-0_0
step 2: ThreadPoolExecutor-1_0
step 3: ThreadPoolExecutor-1_0
on_next: ThreadPoolExecutor-1_0

Desired Output:

starting thread: MainThread
step 1: ThreadPoolExecutor-0_0
step 2: ThreadPoolExecutor-1_0
step 3: MainThread
on_next: MainThread
  • OS: Windows10
  • RxPY: 4.0.2
  • Python version 3.9
@hoc081098
Copy link
Contributor

hoc081098 commented Jul 7, 2022

I think in this case, current_thread_scheduler == threadpool2 is expected behavior,
current_thread means the thread on_next is called in it

@MainRo
Copy link
Collaborator

MainRo commented Jul 19, 2022

This is not possible without an event loop. See the issue #245 for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants