简体   繁体   English

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

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

I have two displays on a Raspberry Pi 2 running Jessie: the (default) HDMI display, and an Adafruit PiTFT LCD. 我在运行Jessie的Raspberry Pi 2上有两个显示器:(默认)HDMI显示器和Adafruit PiTFT LCD。

I can start pygame programs independently in two SSH terminals, for two different displays, EXCEPT the 2nd one I run pauses until I hit ^C. 我可以在两个SSH终端中独立启动pygame程序,以显示两种不同的显示,除了第二个显示,我暂停运行直到碰到^ C为止。

Using SSH on my Mac, I can run a 'count-to-50' pygame program on either one just fine. 在Mac上使用SSH,我可以在其中任意一个上运行“计数到50”的pygame程序。 (The only difference is the LCD version sets 2 env variables that redirect pygame to the LCD screen.) (唯一的区别是LCD版本设置了2个env变量,这些变量将pygame重定向到LCD屏幕。)

If I open two SSH terminals, and try to run both python programs, the second program only starts running if I press control-C. 如果我打开两个SSH终端,并尝试运行两个python程序,则仅当我按Control-C时,第二个程序才开始运行。

What do I need to do differently so the second program starts running normally without first having to press control-C? 我需要做些什么来使第二个程序正常运行而无需先按Ctrl-C键?

I'm not sure if this a "linux" device setup issue, a "python" code issue? 我不确定这是“ linux”设备设置问题还是“ python”代码问题?

Here's the two simple programs: 这是两个简单的程序:

# 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)

and

#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)

On my Mac, I open two SSH terminals to the Raspberry Pi. 在Mac上,我打开两个Raspberry Pi的SSH终端。

In first SSH terminal: 在第一个SSH终端中:

$ sudo python count_on_lcd.py
(starts running normally)

In second SSH terminal: 在第二个SSH终端中:

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

It does the same thing regardless which order I start the two programs... the first command I invoke always runs immediately, the second command I invoke also requires a ^C before it will start running. 无论我启动这两个程序的顺序如何,它所做的事情都是一样的...我调用的第一个命令始终立即运行,而我调用的第二个命令在开始运行之前也需要一个^ C。

This seems to be an issue with pygame.init() not playing nicely if itls already running in a separate SSH terminal. 如果pygame.init()已在单独的SSH终端中运行,则pygame.init()不能很好地播放,这似乎是一个问题。

I found a solution on SO. 我找到了解决方案。 It's not pretty but it works. 它不是很漂亮,但是可以。

Only slightly modified from pygame requires keyboard interrupt to init display pygame稍加修改就需要键盘中断才能显示

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM