Skip to content

Instantly share code, notes, and snippets.

@soda92
Created November 28, 2024 09:45
Show Gist options
  • Save soda92/abd989ba56791b37cb75cb0a833c2440 to your computer and use it in GitHub Desktop.
Save soda92/abd989ba56791b37cb75cb0a833c2440 to your computer and use it in GitHub Desktop.
PyQt webbrowser become a virus

PyQt webbrowser become a virus

I'm trying to combine backend and frontend.

I've created a frontend using PyQt webbrowser:

from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtWebEngineWidgets import *

import sys


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.browser = QWebEngineView()
        self.browser.setBaseSize(800, 600)
        self.browser.setUrl(QUrl("http://127.0.0.1:8000/"))
        self.setCentralWidget(self.browser)
        self.show()


def gui_main():
    app = QApplication(sys.argv)
    window = MainWindow()
    app.exec_()


if __name__ == "__main__":
    gui_main()

The backend is using fastapi:

from typing import Union
from fastapi import FastAPI
app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}

def backend():
    import uvicorn
    uvicorn.run(app)

So far, everything is good. I can launch backend and start debugging: uvicorn backend:app

But when I tried to create a single executable using pyinstaller: pyinstaller --onefile main.py

The main.py just launches backend and frontend:

from backend import backend
from gui import gui_main

import multiprocessing

if __name__ == "__main__":
    p = multiprocessing.Process(target=backend)
    p.start()
    gui_main()

The resulting app keep poping up windows and exausted all memory.

I've tried to restart computer, but the issue remains the same.


A simple search query like "pyinstaller multiprocessing" should suffice: Common Issues and Pitfalls: "A typical symptom of failing to call multiprocessing.freeze_support() before your code [...] attempts to make use of multiprocessing functionality is an endless spawn loop of your application process."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment