When a multiprocessing program runs, dozens of programs run.

Asked 1 years ago, Updated 1 years ago, 288 views

Description of the program you are creating first.

Below is the code you wrote.

main.py--

import chat

import pygame

import random

import glob

import re

from multiprocessing import Process, Queue

if __name__ == "__main__":

    width = 1600
    height = 900
    imagesFolderPath = "./images"
    R = 0
    G = 255
    B = 0
    videoId = ""
    FPS = 60
    paidOutSpeed=5
    paidInSpeed=5

    # Settings File Path
    configFile = "./config.txt"

    pygame.init()

Roughly...

    config.close()

    q = Queue()
    q.put(overComment)

    #Multi-process execution
    th1 = Process(target=chat.fetch_comments, args=(q, videoId))
    th1.start()

    # Set Screen Size
    screen = pygame.display.set_mode((width, height))

    #Pi game screen output, image output

 Let's...



chat.py

import pytchat

def fetch_comments(chatImageNum, videoId):
    chat = pytchat.create(videoId)
    runningChat = True
    while True:
        for c in chat.get().sync_items():
            chatImageNum.put(c.message)

That's all for the code. When th1.start() moves, if name == "main" is constantly recalled to create a new process... A new pie game screen is created.

When running with VSCode, it runs without a problem (no new process is created), but when you create and run an executable file with pyinstaller, there is a problem that the process continues to be created.

Can I know the cause or how to fix it?

multiprocessing python pygame

2023-02-19 22:11

1 Answers

If name== "main": A code block is a code that instructs Python to execute the code within that block if the script runs. This is to prevent the code on the module from being executed when importing the module.

Therefore, using if name=="main"": code block on main.py ensures that th1.start() is executed only when the script is executed. However, when you create an executable file with pyinstaller, this if block may not work correctly.

Therefore, if name== "main" at main.py, you can put the contents of the block inside the main() function and change it to call the main() function below if name== "main": For example:

def main():
    width = 1600
    height = 900
    imagesFolderPath = "./images"
    R = 0
    G = 255
    B = 0
    videoId = ""
    FPS = 60
    paidOutSpeed=5
    paidInSpeed=5

    # Settings File Path
    configFile = "./config.txt"

    pygame.init()

    # ...

    config.close()

    q = Queue()
    q.put(overComment)

    #Multi-process execution
    th1 = Process(target=chat.fetch_comments, args=(q, videoId))
    th1.start()

    # Set Screen Size
    screen = pygame.display.set_mode((width, height))

    #Pi game screen output, image output
    # ...


if __name__ == "__main__":
    main()

This modification can ensure that th1.start() runs only on if name == "main":


2023-02-20 04:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.