gpt4 book ai didi

python - 在第二个 SSH 终端中运行第二个 pygame 程序(用于第二个显示)会暂停,直到按下 ^C

转载 作者:太空宇宙 更新时间:2023-11-04 12:58:19 25 4
gpt4 key购买 nike

我在运行 Jessie 的 Raspberry Pi 2 上有两个显示器:(默认)HDMI 显示器和一个 Adafruit PiTFT LCD。

我可以在两个 SSH 终端独立启动 pygame 程序,用于两个不同的显示,除了我运行的第二个暂停,直到我按下 ^C。

在我的 Mac 上使用 SSH,我可以在任何一个上运行“数到 50”的 pygame 程序。 (唯一的区别是 LCD 版本设置了 2 个将 pygame 重定向到 LCD 屏幕的环境变量。)

如果我打开两个 SSH 终端,并尝试运行两个 python 程序,第二个程序只有在我按下 control-C 时才会开始运行。

我需要做哪些不同的事情才能让第二个程序开始正常运行而无需先按 control-C?

我不确定这是“linux”设备设置问题还是“python”代码问题?

这是两个简单的程序:

# count_on_lcd.py
import pygame, time, os
# on raspberry pi this sends to LCD screen
os.environ['SDL_VIDEODRIVER'] = 'fbcon'
os.environ["SDL_FBDEV"] = "/dev/fb1"
pygame.init()
Screen = max(pygame.display.list_modes())
Surface = pygame.display.set_mode(Screen)
Surface.fill(pygame.Color(0,0,0))
pygame.display.update()
font_big = pygame.font.Font(None, 150)
for i in range(50):
print (i)
Surface.fill(pygame.Color(128,0,0))
text_surface = font_big.render('%s'%i, True, (255,255,255))
rect = text_surface.get_rect(center=(50,50))
Surface.blit(text_surface, rect)
pygame.display.update()
time.sleep(1)
Surface.fill(pygame.Color(0,128,0))
pygame.display.update()
time.sleep(1)

#count_on_hdmi.py
import pygame, time
# on raspberry pi the HDMI screne is default
pygame.init()
Screen = max(pygame.display.list_modes())
Surface = pygame.display.set_mode(Screen)
Surface.fill(pygame.Color(0,0,0))
pygame.display.update()
font_big = pygame.font.Font(None, 150)
for i in range(50):
print (i)
Surface.fill(pygame.Color(128,0,0))
text_surface = font_big.render('%s'%i, True, (255,255,255))
rect = text_surface.get_rect(center=(50,50))
Surface.blit(text_surface, rect)
pygame.display.update()
time.sleep(1)
Surface.fill(pygame.Color(0,128,0))
pygame.display.update()
time.sleep(1)

在我的 Mac 上,我打开两个连接到 Raspberry Pi 的 SSH 终端。

在第一个 SSH 终端中:

$ sudo python count_on_lcd.py
(starts running normally)

在第二个 SSH 终端中:

$ sudo python count_on_hdmi.py
(Pauses until I type ^C)

无论我以何种顺序启动这两个程序,它都会做同样的事情...我调用的第一个命令总是立即运行,我调用的第二个命令也需要一个 ^C 才能开始运行。

最佳答案

如果 pygame.init() 已经在单独的 SSH 终端中运行,这似乎是 pygame.init() 无法正常运行的问题。

我在 SO 上找到了解决方案。它不是很漂亮,但很管用。

仅对 pygame requires keyboard interrupt to init display 稍作修改

from signal import alarm, signal, SIGALRM, SIGKILL

def init_Pygame(surf):

# this section is an unbelievable nasty hack - for some reason Pygame
# needs a keyboardinterrupt to initialise in some limited circs (second time running)
class Alarm(Exception):
pass

def alarm_handler(signum, frame):
raise Alarm

signal(SIGALRM, alarm_handler)
alarm(3)
print("Step 2 ")
try:
pygame.init()
Screen = max(pygame.display.list_modes())
surf = pygame.display.set_mode(Screen)
alarm(0)
except Alarm:
raise KeyboardInterrupt
return (surf)

关于python - 在第二个 SSH 终端中运行第二个 pygame 程序(用于第二个显示)会暂停,直到按下 ^C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34981542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com