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

Python: Add support for invoke_from_event_loop #4757

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions api/python/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ fn quit_event_loop() -> Result<(), errors::PyEventLoopError> {
slint_interpreter::quit_event_loop().map_err(|e| e.into())
}

#[pyfunction]
fn invoke_from_event_loop(callable: PyObject) -> Result<(), errors::PyEventLoopError> {
slint_interpreter::invoke_from_event_loop(move || {
Python::with_gil(|py| {
if let Err(err) = callable.call0(py) {
eprintln!("Error invoking python callable from closure invoked via slint::invoke_from_event_loop: {}", err)
}
})
})
.map_err(|e| e.into())
}

use pyo3::prelude::*;

#[pymodule]
Expand All @@ -42,6 +54,7 @@ fn slint(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<models::PyModelBase>()?;
m.add_function(wrap_pyfunction!(run_event_loop, m)?)?;
m.add_function(wrap_pyfunction!(quit_event_loop, m)?)?;
m.add_function(wrap_pyfunction!(invoke_from_event_loop, m)?)?;

Ok(())
}
29 changes: 29 additions & 0 deletions api/python/tests/test_invoke_from_event_loop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial

from slint import slint as native
import threading
from datetime import timedelta


def test_threads():
global was_here
was_here = False

def invoked_from_event_loop():
global was_here
was_here = True
native.quit_event_loop()

def quit():
native.invoke_from_event_loop(invoked_from_event_loop)

thr = threading.Thread(target=quit)
native.Timer.single_shot(timedelta(milliseconds=10), lambda: thr.start())
fallback_timer = native.Timer()
fallback_timer.start(native.TimerMode.Repeated, timedelta(
milliseconds=100), native.quit_event_loop)
native.run_event_loop()
thr.join()
fallback_timer.stop()
assert was_here == True
Loading