【问题标题】:running 2nd pygame program (for 2nd display) in 2nd SSH terminal pauses until ^C is pressed在第二个 SSH 终端中运行第二个 pygame 程序(用于第二个显示)会暂停,直到按下 ^C
【发布时间】:2016-01-24 21:13:44
【问题描述】:

我在运行 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 才能开始运行。

【问题讨论】:

  • 可能使用&在后台运行程序 -> $ python count_on_lcd.py &
  • 如果您启动第三个 SSH 会话,第二个会话在做什么?你能strace它看看它被阻止了吗?我无法想象这实际上是一个 Python 问题,因为 CTRL+C 可能会退出 Python,而不是把它从卡住的东西中解脱出来并让它继续。

标签: python linux raspberry-pi pygame


【解决方案1】:

如果 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)

【讨论】:

    猜你喜欢
    • 2017-08-06
    • 2018-03-17
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2013-01-09
    • 2022-08-13
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多