Skip to content

Instantly share code, notes, and snippets.

@philipbel
Created March 28, 2018 21:15
Show Gist options
  • Save philipbel/a64bcc11114f1685086b61c9d5490a5b to your computer and use it in GitHub Desktop.
Save philipbel/a64bcc11114f1685086b61c9d5490a5b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import PyQt5
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton
import rx
import sys
import requests
import logging
log = logging.getLogger()
log.setLevel(logging.DEBUG)
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
fmt = logging.Formatter('[0x%(thread)x %(asctime)s] %(module)s:%(funcName)s: %(message)s')
sh.setFormatter(fmt)
log.addHandler(sh)
def do_something():
log.debug("Creating Observable")
def observable(observer):
log.debug("Running")
try:
r = requests.get('http://google.com')
except Exception as e:
observer.on_error(e)
else:
observer.on_next(r.content)
observer.on_completed()
return rx.Observable.create(observable)
def main(args):
log.debug("Main")
def on_content_received(content):
log.debug("Content received")
mw.statusBar().showMessage("Size: {} byte(s)".format(len(content)))
def on_click():
log.debug("Button clicked")
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"))
app = QApplication(args)
qtsch = rx.concurrency.QtScheduler(PyQt5.QtCore)
tpsch = rx.concurrency.ThreadPoolScheduler(4)
mw = QMainWindow()
button = QPushButton("Click me", mw)
button.clicked.connect(on_click)
mw.show()
return app.exec_()
if __name__ == '__main__':
sys.exit(main(sys.argv))
@leinardi
Copy link

leinardi commented Aug 4, 2018

Hi, have you found a solution for the error QObject::startTimer: Timers can only be used with threads started with QThread?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment