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

时间:2016-01-24 21:13:45

标签: python linux raspberry-pi pygame

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

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

在我的Mac上使用SSH,我可以在任何一个上运行'count-to-50'pygame程序就好了。 (唯一的区别是LCD版本设置2个env变量,将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上,我打开两个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)

无论我启动两个程序的顺序如何都是一样的...我调用的第一个命令总是立即运行,我调用的第二个命令在开始运行之前也需要^ C.

1 个答案:

答案 0 :(得分:0)

这似乎是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)