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

QObject::startTimer: Timers can only be used with threads started with QThread #233

Closed
philipbel opened this issue Mar 28, 2018 · 3 comments

Comments

@philipbel
Copy link

philipbel commented Mar 28, 2018

Hello.

I am trying to use QtScheduler in conjunction with ThreadPoolScheduler, the idea being to perform network operations in background threads, and then to call back into the UI.

do_something() \
    .subscribe_on(tpsch) \
    .observe_on(qtsch) \
    .subscribe(on_next=on_content_received,
                on_error=lambda e: print("error: {}".format(e)),
                on_completed=lambda: print("completed"))

I get this error from _qtimer_schedule:

QObject::startTimer: Timers can only be used with threads started with QThread

Please, see this gist for a test case.

Is this behavior expected?

@benjaminHanalytics
Copy link

any solutions?

@jcafhe
Copy link
Collaborator

jcafhe commented Feb 10, 2019

Hello,
better late than never, the QtScheduler can not be 'mixed' with schedulers that schedule elements on an other thread than the Qt thread. This is because QtScheduler is based on QTimer which is not thread safe and cannot be started from another thread (more specifically outside of a QThread).

So you have to specify for each operator in your chain that accepts a scheduler as argument your QtScheduler. In RxPY3 (wip), you will specify the desired scheduler in subscribe method for all the upstream chain. This is decribed here.

Alternatively, I've been working on a QtScheduler which should be thread safe. It's available here:
https://github.com/jcafhe/RxPY/tree/qt-threadsafe
It defines a new scheduler in rx.concurrency.mainloopscheduler.qtthreadsafe which is based on QtCore.QCoreApplication.postEvent(). There are some examples in rx/examples/qtthreadsafe. Keep in mind it's just a prototype and may be out of date but please feel free to improve it.

@wuyuanyi135
Copy link

@jcafhe Thank you. Your thread-safe version of QtScheduler works well. Can you please elaborate what "thread-safe" means in the context of rx? I ran into the similar problem of AsyncioThreadSafeScheduler and AsyncioScheduler before.

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

No branches or pull requests

5 participants