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

running 2nd pygame program (for 2nd display) in 2nd SSH terminal pauses until ^C is pressed

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

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

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

如果我打开两个 SSH 终端,并尝试 运行 两个 python 程序,如果我按 control-C,第二个程序只会启动 运行ning。

我需要做哪些不同的事情才能使第二个程序正常启动 运行ning 而无需先按 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 上,我打开两个 SSH 终端到 Raspberry Pi。

在第一个 SSH 终端中:

$ sudo python count_on_lcd.py
(starts running normally)

在第二个 SSH 终端中:

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

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

这似乎是 pygame.init() 的一个问题,如果它已经 运行 在一个单独的 SSH 终端中,则不能很好地播放。

我在 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)