寻求帮助以使此代码更好(更好)?循环运行一段时间后(15-20分钟),程序冻结。一些用户告诉我,使用time.sleep()
进行同步并不是一个好主意,我想知道这是不是我的问题。回复时请使用我的代码作为示例,因为我是Python的新手。越简越好。
代码应该默认播放视频循环,当触发按钮(RPi / GPIO)时,播放不同的视频。该视频完成后,将返回默认的循环视频。我正在使用OMXPlayer。
#!/usr/bin/python
from time import sleep
import RPi.GPIO as GPIO
import subprocess
import time
import thread
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup(4, GPIO.IN) # Button 1
GPIO.setup(9, GPIO.IN) # Button 2
GPIO.setup(10, GPIO.IN) # Button 3
GPIO.setup(11, GPIO.IN) # Button 4
GPIO.setup(17, GPIO.IN) # Button 5
GPIO.setup(22, GPIO.IN) # Button 6
GPIO.setup(27, GPIO.IN) # Button 7
video_playing = False # Loop flag initial status
def welcome_loop():
global playProcess
while True:
x = 1
if not video_playing:
print "Play Welcome Video"
time.sleep(.05)
playProcess=subprocess.Popen(['omxplayer','-b','Desktop/videos/loop/loop.mp4'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
time.sleep(24) # Length of video
x += 1
def videos():
#initialise a previous input variable to 0 (assume button not pressed last)
prev_input = 0
global video_playing
while True:
button1 = GPIO.input(27)
if ((not prev_input) and button1):
video_playing = True # Set the flag (loop pauses)
print "Stop Welcome Video"
time.sleep(.5)
playProcess.stdin.write('q') # Stop video
time.sleep(.5)
print "Play Martin Lab - Reel"
martinlab_reel=subprocess.Popen(['omxplayer','-b','Desktop/videos/martinlab_reel.mp4'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
time.sleep(30) # Length of video
martinlab_reel.stdin.write('q')
video_playing = False # Unset the flag (loop continues)
#update previous input
prev_input = button1
#slight pause to debounce
time.sleep(.25)
button2 = GPIO.input(9)
if ((not prev_input) and button2):
video_playing = True # Set the flag (loop pauses)
print "Stop Welcome Video"
time.sleep(.5)
playProcess.stdin.write('q') # Stop video
time.sleep(.5)
print "Play Shoppingcart"
shoppingcart=subprocess.Popen(['omxplayer','-b','Desktop/videos/shoppingcart.mp4'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
time.sleep(111) # Length of video
shoppingcart.stdin.write('q')
video_playing = False # Unset the flag (loop continues)
#update previous input
prev_input = button2
#slight pause to debounce
time.sleep(.25)
button3 = GPIO.input(10)
if ((not prev_input) and button3):
video_playing = True # Set the flag (loop pauses)
print "Stop Welcome Video"
time.sleep(.5)
playProcess.stdin.write('q') # Stop video
time.sleep(.5)
print "Play Dodgeballs"
dodgeballs=subprocess.Popen(['omxplayer','-b','Desktop/videos/dodgeballs.mp4'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
time.sleep(102) # Length of video
dodgeballs.stdin.write('q')
video_playing = False # Unset the flag (loop continues)
#update previous input
prev_input = button3
#slight pause to debounce
time.sleep(.25)
button4 = GPIO.input(11)
if ((not prev_input) and button4):
video_playing = True # Set the flag (loop pauses)
print "Stop Welcome Video"
time.sleep(.5)
playProcess.stdin.write('q') # Stop video
time.sleep(.5)
print "Play Hidden Camera"
hiddencamera=subprocess.Popen(['omxplayer','-b','Desktop/videos/hiddencamera.mov'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
time.sleep(37) # Length of video
hiddencamera.stdin.write('q') # Stop video
video_playing = False # Unset the flag (loop continues)
#update previous input
prev_input = button4
#slight pause to debounce
time.sleep(.25)
button5 = GPIO.input(17)
if ((not prev_input) and button5):
video_playing = True # Set the flag (loop pauses)
print "Stop Welcome Video"
time.sleep(.5)
playProcess.stdin.write('q') # Stop video
time.sleep(.5)
print "Play Light of Human Kindness"
lohk_reel=subprocess.Popen(['omxplayer','-b','Desktop/videos/LOHK_short.mp4'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
time.sleep(83) # Length of video
lohk_reel.stdin.write('q')
video_playing = False # Unset the flag (loop continues)
#update previous input
prev_input = button5
#slight pause to debounce
time.sleep(.25)
button6 = GPIO.input(22)
if ((not prev_input) and button6):
video_playing = True # Set the flag (loop pauses)
print "Stop Welcome Video"
time.sleep(.5)
playProcess.stdin.write('q') # Stop video
time.sleep(.5)
print "Play RVA Makerfest"
rva_makerfest=subprocess.Popen(['omxplayer','-b','Desktop/videos/rva_makerfest.mp4'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
time.sleep(101) # Length of video
rva_makerfest.stdin.write('q')
video_playing = False # Unset the flag (loop continues)
#update previous input
prev_input = button6
#slight pause to debounce
time.sleep(.25)
button7 = GPIO.input(27)
if ((not prev_input) and button7):
video_playing = True # Set the flag (loop pauses)
print "Stop Welcome Video"
time.sleep(.5)
playProcess.stdin.write('q') # Stop video
time.sleep(.5)
print "Play PartyBot - Reel"
partybot_reel=subprocess.Popen(['omxplayer','-b','Desktop/videos/partybot_reel.mov'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
time.sleep(61) # Length of video
partybot_reel.stdin.write('q')
video_playing = False # Unset the flag (loop continues)
#update previous input
prev_input = button7
#slight pause to debounce
time.sleep(.25)
thread.start_new_thread( videos, () ) # Videos thread
thread.start_new_thread( welcome_loop, () ) # Loop thread
while True:
pass
GPIO.cleanup() #Reset GPIOs
错误(运行2小时后):
Unhandled exception in thread started by <function welcome_loop at 0xb6c8b3b0>
Traceback (most recent call last):
File "./labmural2.py", line 32, in welcome_loop
stderr=subprocess.PIPE,close_fds=True)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1153, in _execute_child
self.pid = os.fork()
OSError: [Errno 12] Cannot allocate memory
快速返回值(内存使用率):
Partition of a set of 26904 objects. Total size = 1957148 bytes.
Index Count % Size % Cumulative % Kind (class / dict of class)
0 12601 47 786404 40 786404 40 str
1 6274 23 254548 13 1040952 53 tuple
2 1756 7 126432 6 1167384 60 types.CodeType
3 80 0 122944 6 1290328 66 dict of module
4 203 1 108292 6 1398620 71 dict of type
5 1709 6 102540 5 1501160 77 function
6 121 0 98540 5 1599700 82 dict (no owner)
7 203 1 90232 5 1689932 86 type
8 121 0 70316 4 1760248 90 dict of class
9 1054 4 42160 2 1802408 92 __builtin__.wrapper_descriptor
free -m :
total used free shared buffers cached
Mem: 247 159 87 0 7 75
-/+ buffers/cache: 76 170
Swap: 99 8 91
答案 0 :(得分:0)
使用线程和队列代替线程和全局变量。
答案 1 :(得分:0)
由于您启动进程并等待它们完成的方式,您可能会因内存或可用进程耗尽而耗尽内存。程序运行一段时间后,使用ps -AF
检查正在运行的进程数。
想象一下,您尝试播放的视频运行时间为9秒而不是10秒。这将导致系统在第一个视频播放完成之前由于time.sleep()
呼叫而尝试再次开始视频播放。最终,你会遇到许多重叠的视频播放器试图运行相同的视频,因此耗尽内存或操作系统允许的进程数。
我建议将play / wait结构更改为:
playProcess=subprocess.Popen(['omxplayer','-b','Desktop/videos/loop/loop.mp4'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
playProcess.wait() # Wait for video to finish playing
答案 2 :(得分:0)
我有这个问题,当在linux中检查打开文件时,我了解根据时间增加文件数量。所以文件数到达“ulimit -n”脚本后停止。我的解决方案是在linux中增加打开文件的数量,并且在链接下面使用增加文件数到1,000,000:
https://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/
但基本上解决方法是防止打开很多文件。 (我只是用一个类读取温度,我不知道为什么根据时间增加文件数量)