Skip to content

Should workers support py-* events? #1763

@antocuni

Description

@antocuni

This is a sub-issue of #1762.
I expect some discussion here, so better to open a separate one.

Currently, we have 4 tests which works in the main thread but not in the worker:

@skip_worker("NEXT: py-click doesn't work")
def test_no_explicit_target(self):
self.pyscript_run(
"""
<script type="py">
from pyscript import display
def display_hello(error):
display('hello world')
</script>
<button id="my-button" py-click="display_hello">Click me</button>
"""
)
self.page.locator("button").click()
text = self.page.locator("script-py").text_content()
assert "hello world" in text

@skip_worker("NEXT: display target does not work properly")
def test_explicit_target_on_button_tag(self):
self.pyscript_run(
"""
<script type="py">
from pyscript import display
def display_hello(error):
display('hello', target='my-button')
</script>
<button id="my-button" py-click="display_hello">Click me</button>
"""
)
self.page.locator("text=Click me").click()
text = self.page.locator("id=my-button").inner_text()
assert "hello" in text

@skip_worker("NEXT: py-click doesn't work inside workers")
def test_python_exception_in_event_handler(self):
self.pyscript_run(
"""
<button py-click="onclick">Click me</button>
<script type="py">
def onclick(event):
raise Exception("this is an error inside handler")
</script>
"""
)
self.page.locator("button").click()
self.wait_for_console(
"Exception: this is an error inside handler", match_substring=True
)
self.check_py_errors("Exception: this is an error inside handler")
## error in console
tb_lines = self.console.error.lines[-1].splitlines()
assert tb_lines[0] == "PythonError: Traceback (most recent call last):"
## error in DOM
tb_lines = self.page.locator(".py-error").inner_text().splitlines()
assert tb_lines[0] == "Traceback (most recent call last):"
assert tb_lines[-1] == "Exception: this is an error inside handler"

@skip_worker("NEXT: py-click doesn't work inside workers")
def test_py_attribute_without_id(self):
self.pyscript_run(
"""
<button py-click="myfunc">Click me</button>
<script type="py">
def myfunc(event):
print("hello world!")
</script>
"""
)
btn = self.page.wait_for_selector("button")
btn.click()
self.wait_for_console("hello world!")
assert self.console.log.lines[-1] == "hello world!"
assert self.console.error.lines == []

They all share the same fundamental reason:

<button py-click="myfunc">Click me</button>
<script type="py" worker>
    def myfunc(event):
        print("hello world!")
</script>

Should this work or not?
Let me try to play devil's advocate for both camps.

Team "it should not work"

One reason for saying "not" is that it's unclear what to do in case of multiple workers, or maybe main-thread+workers, e.g.:

<button py-click="myfunc">Click me</button>
<script type="py" src="aaa.py"></script>
<script type="py" src="bbb.py" worker></script>
<script type="py" src="ccc.py" worker></script>

In a situation like the above, we have three different interpreters: one in the main thread and one for each of the two separate workers, and it's not obvious/clear/well defined where to search and find myfunc, so better to avoid it entirely.

Team "it should work"

In case there is only one interpreter running in a worker, it is obvious where to look.
Moreover, we make it easier to switch from "main thread" to "worker", and it makes the two cases behaving more similarly.

Also, I expect that in the majority of cases people will have a single interpreter running anyway (either main thread or worker), so this is a solution which "just works" for the common case.

In case of multiple interpreters, we should think of a more explicit syntax and/or require them to use @when.

Personally, I am in the "it should work camp" :)

/cc @WebReflection @ntoll @tedpatrick @JeffersGlass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions