Python 子进程消耗内存

问题描述 投票:0回答:1

我试图通过按树莓派帽子上的左/右按钮来设置广播频率。我使用 PiFmRds 库。

但是在更改频率几次后,Pi 内存不足,我得到“无法分配内存”。错误。旧的子进程是否继续运行并消耗内存?如果,我该如何杀死旧的子进程?

谢谢!!!

(代码有点乱,抱歉;))

import os
import board
import busio
import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd
import time
import subprocess
import sys
import digitalio
import threading
import signal


lcd_columns = 16
lcd_rows = 2
i2c = busio.I2C(board.SCL, board.SDA)
lcd = character_lcd.Character_LCD_RGB_I2C(i2c, lcd_columns, lcd_rows)

freq = 103.3

while True:
    try:
        freq = round(freq, 1)
        bashCommand = "sudo ./pi_fm_rds -audio sound.wav -pi 0000 -freq " + str(freq)

        lcd.clear()
        lcd.message = str(freq)
        transmit.send_signal(signal.SIGINT)

        transmit = subprocess.Popen([bashCommand], stdout=subprocess.PIPE, shell=True)

        print(freq)

        while lcd.left_button or lcd.right_button == False:
            if lcd.left_button:
                freq = freq - 0.1
                while lcd.left_button == True:
                    pass
                break
        
            if lcd.right_button:
                freq = freq + 0.1
                while lcd.right_button == True:
                    pass
                break

    except ValueError:
        print(ValueError)
        exit()
python multithreading subprocess adafruit
1个回答
0
投票

你好,你好吗?关于您的问题,在我看来,在您提供的代码中,执行的进程仍然加载在内存中。每次更改频率时,都会创建新进程,而不会完成前一个进程。

#You must start the transmit at the beginning first to guarantee a clean channel

# Later add a while condition to avoid process overload.

while True:
    try:
   
   # Before starting a new process, terminate the previous one if it exists
        if transmit is not None:
            transmit.terminate()
            transmit.wait()  # whait until the proccess finish 
        transmit = subprocess.Popen([bashCommand], stdout=subprocess.PIPE, shell=True)
    except ValueError:
        print(ValueError)
        exit()
© www.soinside.com 2019 - 2024. All rights reserved.