Git Product home page Git Product logo

Comments (11)

r0x0r avatar r0x0r commented on July 28, 2024

The problem with multiprocessing is that a Window object cannot be serialised because of threading events. Unability to serialise Window is rather a show stopper. Non-blocking start() can be implemented without multiprocessing on all the platforms but Cocoa.
I have tried to implement this, but could not find an adequate solution. Help is welcomed.

from pywebview.

louisnw01 avatar louisnw01 commented on July 28, 2024

The problem with multiprocessing is that a Window object cannot be serialised because of threading events. Unability to serialise Window is rather a show stopper. Non-blocking start() can be implemented without multiprocessing on all the platforms but Cocoa. I have tried to implement this, but could not find an adequate solution. Help is welcomed.

Why do you need to serialise the Window object?

Louis

from pywebview.

r0x0r avatar r0x0r commented on July 28, 2024

You might want to access a Window objects in your application code. For that you need to pass them between processes and they need to be serialised first.

from pywebview.

louisnw01 avatar louisnw01 commented on July 28, 2024

You might want to access a Window objects in your application code. For that you need to pass them between processes and they need to be serialised first.

What I mean is the Window object (in its current state) can sit in the other process.

Then another 'window' object (which would replace the old one) could be used which only communicates with the other object running in the other process

from pywebview.

louisnw01 avatar louisnw01 commented on July 28, 2024

Essentially all you would need to write is a wrapper for a new window class, and a controller which sits in the other process and keeps everything up to date

from pywebview.

r0x0r avatar r0x0r commented on July 28, 2024

I am afraid I do not follow. How would you rewrite following code in multiprocessing? This is identical to passing a function with a parameter to start

from time import sleep
from threading import Thread
import webview

"""
Loading new HTML after the window is created
"""


def load_html(window):
    sleep(5)
    window.load_html('<h1>This is dynamically loaded HTML</h1>')


if __name__ == '__main__':
    window = webview.create_window('Load HTML Example', html='<h1>This is initial HTML</h1>')

    t = Thread(target=load_html, args=(window,))
    t.start()
    webview.start()

from pywebview.

louisnw01 avatar louisnw01 commented on July 28, 2024

I am afraid I do not follow. How would you rewrite following code in multiprocessing? This is identical to passing a function with a parameter to start

from time import sleep
from threading import Thread
import webview

"""
Loading new HTML after the window is created
"""


def load_html(window):
    sleep(5)
    window.load_html('<h1>This is dynamically loaded HTML</h1>')


if __name__ == '__main__':
    window = webview.create_window('Load HTML Example', html='<h1>This is initial HTML</h1>')

    t = Thread(target=load_html, args=(window,))
    t.start()
    webview.start()

Something like this:

from time import sleep
from threading import Thread
import webview


#
#  this runs in a seperate process
#
class ActualWindow:
    
    ...

    def loop(self):
        while 1:
            method_name, args = self.mp_queue.get()
            method = getattr(self, method_name)
            method(*args)


#
#  dummy Window class, which is instansiated in the main process
#
class Window():
    
    ...

    def load_html(self, html: str):
        self.mp_queue.put(('load_html', (html)));


if __name__ == '__main__':
    window = webview.create_window('Load HTML Example', html='<h1>This is initial HTML</h1>')

    webview.start(block=False)

    sleep(5)
    window.load_html('<h1>This is dynamically loaded HTML</h1>')

from pywebview.

r0x0r avatar r0x0r commented on July 28, 2024

The thing is pywebview itself is using window objects internally. In your example you create a window object in the main process. How do you pass it to the process in webview.start()?

from pywebview.

louisnw01 avatar louisnw01 commented on July 28, 2024

The thing is pywebview itself is using window objects internally. In your example you create a window object in the main process. How do you pass it to the process in webview.start()?

The 'actual' window object is the one running in the other process ie the same process that webview is running in.

The only communication between the two processes is the 'dummy' classes or methods that send over a queue, which control the actual classes instantiated in the other process

from pywebview.

lanzz avatar lanzz commented on July 28, 2024

If all that is needed is a non-blocking webview.start(), then why not just keep the current threading implementation, but instead of running the backend logic callback in a thread, just create the main window in a thread and return immediately, allowing the backend logic to be implemented in the main thread? Considering that webview.start() will already start all windows but the first in new threads, it's even a bit unexpected that it starts the first one in the main thread, requiring a separate callback for the backend logic to run that in a thread.

from pywebview.

r0x0r avatar r0x0r commented on July 28, 2024

@louisnw01 honestly I don't know if it is possible to split Window object in such a way. If you or somebody else get it working keeping API the same I can accept a PR.

@lanzz Cocoa requires GUI to be run on the main thread. Other platforms can be run in a thread as you described.

from pywebview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.