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

Stop reactor gracefully on QApplication quit #2

Merged
merged 1 commit into from
Nov 5, 2015

Conversation

estan
Copy link
Contributor

@estan estan commented Nov 4, 2015

If the reactor is running after the QApplication has quit, stop it and run all pending timed calls one last time. This will also allow the 'shutdown' system event to trigger properly, which ReactorBase uses internally to stop its threadpool.

Example:

from __future__ import print_function
from sys import argv

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTimer

import qt5reactor

if __name__ == '__main__':
    app = QApplication(argv)

    qt5reactor.install()

    from twisted.internet import reactor

    reactor.addSystemEventTrigger('before', 'shutdown', lambda: print('before'))
    reactor.addSystemEventTrigger('during', 'shutdown', lambda: print('during'))
    reactor.addSystemEventTrigger('after', 'shutdown', lambda: print('after'))

    QTimer.singleShot(1000, app.quit)  # Quit app after 1 second.

    reactor.run()

Before, this would not print anything. After this change it will correctly print:

before
during
after

If the reactor is running after the QApplication has quit, stop it and
run all pending timed calls one last time. This will also allow the
'shutdown' system event to trigger properly, which ReactorBase uses
internally to stop its threadpool.

Example:

```python
from __future__ import print_function
from sys import argv

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTimer

import qt5reactor

if __name__ == '__main__':
    app = QApplication(argv)

    qt5reactor.install()

    from twisted.internet import reactor

    reactor.addSystemEventTrigger('before', 'shutdown', lambda: print('before'))
    reactor.addSystemEventTrigger('during', 'shutdown', lambda: print('during'))
    reactor.addSystemEventTrigger('after', 'shutdown', lambda: print('after'))

    QTimer.singleShot(1000, app.quit)  # Quit app after 1 second.

    reactor.run()
```

Before, this would not print anything. After this change it will
correctly print:

before
during
after
@sunu
Copy link
Member

sunu commented Nov 4, 2015

Hi @estan,

This looks good to me. But this reminds me that we don't have anything similar to gtrial and GUI tests from qtreactor yet.

For now, I'll just check whether trial is satisfied or not and merge. I have added issues #3 and #4 to improve the current state.

@estan
Copy link
Contributor Author

estan commented Nov 4, 2015

Excellent.

sunu added a commit that referenced this pull request Nov 5, 2015
Stop reactor gracefully on QApplication quit
@sunu sunu merged commit 6c7cd0d into twisted:master Nov 5, 2015
@sunu
Copy link
Member

sunu commented Nov 5, 2015

Thanks @estan!

mFoxRU added a commit to mFoxRU/qtreactor that referenced this pull request Dec 27, 2015
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

Successfully merging this pull request may close these issues.

2 participants