-
Hello everyone, class UvicornServer(multiprocessing.Process):
def __init__(self, config: Config):
super().__init__()
self.server = Server(config=config)
self.config = config
def stop(self):
self.terminate()
def run(self, *args, **kwargs):
self.server.run()
def start_window(shared_dict, url_to_load):
def on_closed():
shared_dict['window'] = 'closed'
win = webview.create_window('Demo', url=url_to_load)
win.events.closed += on_closed
shared_dict['window'] = win
webview.start(storage_path=tempfile.mkdtemp())
app = FastAPI()
@app.get('/')
def read_root():
return {'Hello': 'World'}
@ui.page('/minimize')
def minimize_window():
# Access the shared window object
window = shared_dict['window']
print(window)
if window:
window.minimize()
@ui.page('/show')
def show():
ui.image('https://picsum.photos/id/377/640/360')
ui.run_with(app)
if __name__ == '__main__':
server_ip = "127.0.0.1"
server_port = 8081
manager = multiprocessing.Manager()
shared_dict = manager.dict()
windowsp = multiprocessing.Process(target=start_window, args=(shared_dict, f'http://{server_ip}:{server_port}/show'))
windowsp.start()
config = Config("main:app", host=server_ip, port=server_port, log_level="debug")
instance = UvicornServer(config=config)
instance.start()
while True:
pass
instance.stop() My question is, how to get the window object from the Full traceback:
I have no idea if this is the right method to get the window object, I tried So is using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You are confusing threads with processes. Getting a window in a multi-threaded environment should not be a problem, but sharing data between processes is rather tricky. The error you get means that |
Beta Was this translation helpful? Give feedback.
You are confusing threads with processes. Getting a window in a multi-threaded environment should not be a problem, but sharing data between processes is rather tricky. The error you get means that
window
object cannot be pickled as required by multiprocessing. You may have luck with an alternative pickling library likedill
, see https://stackoverflow.com/questions/19984152/what-can-multiprocessing-and-dill-do-together for details. I have not tested any of this, but from my experience with passing data in a multiprocessing environment in Python is rather painful.