PyGame seems to be intercepting SIGINT from Ctrl-C - python

If I attempt to black out the screen using PyGame, PyGame seems to intercept SIGINT and I cannot use control-C to exit my program. If I simplify my code to the most basic pieces, it looks like this:
import signal
import time
import pygame
class Foo:
def __init__(self):
self.bgcolor = [0,0,0]
pygame.display.init()
pygame.font.init()
pygame.mouse.set_visible(False)
size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
self.blank_screen()
def blank_screen(self):
self.screen.fill(self.bgcolor)
pygame.display.update()
#pygame.quit()
def sighandle(self, signal, frame):
print('I got a ctrl-c')
self.close()
def close(self):
print('calling close')
pygame.quit()
def run(self):
print('I am running')
time.sleep(10)
if __name__=='__main__':
bar = None
try:
bar = Foo()
signal.signal(signal.SIGINT, bar.sighandle)
signal.signal(signal.SIGTERM, bar.sighandle)
bar.run()
finally:
print('in finally block')
if bar is not None:
bar.close()
If I remove the pygame parts, ctrl-c works as expected, and sighandle()'s message is printed. When I put the pygame parts in, the screen blanks as expected, but ctrl-c is ignored, and sighandle()'s message is never printed. The code just runs to completion. Furthermore, if I uncomment the commented out pygame.quit() in blank_screen(), ctrl-c works again, but of course the blacked out screen goes away.
Why is the code ignoring ctrl-c when I invoke PyGame, and how do I make it work as I expect, so that ctrl-c kills the program through sighandle?

After some research, I found that this is indeed the case by design. I don't see it explicitly mentioned in PyGame's documentation, though.
Ctrl-C can be enabled in the code above by handling it as an event. I can modify Foo.run() as such:
def run(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.sighandle()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_c and pygame.key.get_mods() & pygame.KMOD_CTRL:
print "pressed CTRL-C as an event"
self.sighandle()
print('I am running')
time.sleep(10)
I took the PyGame based screen blanking code from Adafruit's VideoLooper, which also included the signal library based handling of SIGINT. But I don't think their code will handle Ctrl-C as-is either.

Related

Python threading crashing on asset load in PyGame

I have constructed some code to load assets into a character class. This works fine in a single thread, but I want to have a loading screen when the assets are loading.
I put the asset loading into a function, and tried to make it run in a separate thread. The issue is that it now hangs, and seemingly does not even execute the main thread.
I've kept an eye on the memory usage, and the memory is well-behaved (never goes above 500M out of 1976M, bits or bytes I'm not sure, it's whatever PyCharm reports).
import threading
import os
import pygame
from pygame.locals import *
def load_assets(screen: pygame.Surface, results: List):
print("thread: started to load assets in thread")
appearances1 = {} # some dictionary, Dict[str, Dict[str, str]]
default_outfit = "office_wear"
print("thread: instance of game class to be created")
game = Game("lmao", screen) # crashes somewhere here when running multithreaded
print("thread: game initialized") # this is never achieved
# rest of function not relevant
results.append(game)
def main():
# ====== INITIALIZE PYGAME ======
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((1024, 768))
clock = pygame.time.Clock()
loading = create_loading_screen(screen)
loading.display(screen)
pygame.display.flip()
# ====== START OTHER THREAD FOR LOADING ASSETS ======
results = []
x = threading.Thread(
target=load_assets, args=(screen, results), daemon=True
)
x.start()
while x.is_alive():
# never see this print statement!
print("x is fine") # want to blit loading screen here
# if I comment out the threading, and run the following instead, my game boots up just fine
# in under 3-4 seconds of waiting for the assets to load
# load_assets(screen, results)
game = results[0]
# other code here to render the game, no more threading after this point
while 1:
for event in pygame.event.get():
if event.type == QUIT:
return -1
game.main()
pygame.display.flip()
clock.tick(game.fps)
if __name__ == '__main__':
main()
You can see the print statements here (everything hangs and we don't even make it to the main thread!)
I figured out the issue. I feel really dumb now... The issue was my Game initializer was touching the pygame.display() which I already knew it shouldn't... I need a nap...

Bizarre behaviour of Pygame mixed with Multiprocessing

I am working on a small project including Raspberry Pi camera. I am using multiprocessing on the PC to perform various tasks and one of them is displaying images in a Pygame window. I have manager to minimize the code suffering the problem. This is the main function, which just creates a Game process, runs it and waits for it to end:
# main.py
from multiprocessing import Manager
import game
with Manager() as manager:
namespace = manager.Namespace()
namespace.done = False
jobs = [
game.Game(namespace),
]
for job in jobs:
job.start()
for job in jobs:
job.join()
Below is the code of the Game class. The state is shared because there will be other processes checking if the program has finished.
# game.py
import multiprocessing
import pygame
import time
import sys
class Game(multiprocessing.Process):
def __init__(self, the_state):
super(Game, self).__init__()
pygame.init()
self.screen = pygame.display.set_mode((100, 100))
self.state = the_state
def handle_key_events(self):
""" Wrapper for handling key events """
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.state.done = True
return
def run(self):
while True:
self.handle_key_events()
if self.state.done:
print('exiting PyGame thread')
pygame.display.quit()
pygame.quit()
return
The behaviour right now is that it doesn't end on clicking the Pygame's exit button. However, if I comment out the pygame.quit() line from Game's run method, it gives me the following message:
exiting PyGame thread
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
after 120 requests (120 known processed) with 1 events remaining.
I tried googling that error but little success. I can't shake off the feeling that I'm attempting something I shouldn't be, considering that probably the Pygame's window is a process on its own.
I would appreciate some insight and recommendations on where to read up
As Blckknght suggested, the issue was that the parent process (main.py) initiated PyGame's window, while the child process (game.py) attempted to quit it. It can be resolved by moving this part:
pygame.init()
self.screen = pygame.display.set_mode((100, 100))
into Game's run method

Raspberry Pi and pygame.error: video system not initialized

I'm trying to run pygame on my Raspberry Pi Zero W, which has a PS4 controller hooked up to it. I found some code that should work but I get this error when I try to python3 game.py:
Traceback (most recent call last):
File "controller.py", line 74, in
ps4.listen()
File "controller.py", line 52, in listen
for event in pygame.event.get():
pygame.error: video system not initialized
The same code from someone else on Stackoverflow got it to work (at least that's what I assume), but he had a different problem, yet the same code. I did try to run that code instead, but I got the same error. I tried all suggestions I could find from Stackoverflow, but none of them worked. Here's the code I found:
import os
import pprint
import pygame
class PS4Controller(object):
"""Class representing the PS4 controller. Pretty straightforward functionality."""
controller = None
axis_data = None
button_data = None
hat_data = None
def init(self):
"""Initialize the joystick components"""
pygame.init()
pygame.joystick.init()
self.controller = pygame.joystick.Joystick(0)
self.controller.init()
def listen(self):
"""Listen for events to happen"""
if not self.axis_data:
self.axis_data = {}
if not self.button_data:
self.button_data = {}
for i in range(self.controller.get_numbuttons()):
self.button_data[i] = False
if not self.hat_data:
self.hat_data = {}
for i in range(self.controller.get_numhats()):
self.hat_data[i] = (0, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.JOYAXISMOTION:
self.axis_data[event.axis] = round(event.value,2)
elif event.type == pygame.JOYBUTTONDOWN:
self.button_data[event.button] = True
elif event.type == pygame.JOYBUTTONUP:
self.button_data[event.button] = False
elif event.type == pygame.JOYHATMOTION:
self.hat_data[event.hat] = event.value
# Insert your code on what you would like to happen for each event here!
# In the current setup, I have the state simply printing out to the screen.
os.system('clear')
pprint.pprint(self.button_data)
pprint.pprint(self.axis_data)
pprint.pprint(self.hat_data)
if __name__ == "__main__":
ps4 = PS4Controller()
ps4.init()
ps4.listen()
Any clue what to do and why it's not working? I run this on Jessie Lite, so there is no desktop or anything like that.
pygame.init fails silently when a module cannot be initialized:
No exceptions will be raised if a module fails, but the total number if successful and failed inits will be returned as a tuple. You can always initialize individual modules manually, but pygame.init()initialize all imported pygame modules is a convenient way to get everything started. The init() functions for individual modules will raise exceptions when they fail.
In your case, it didn't initialize the display. To have it fail loudly, call pygame.display.init explicitly:
import pygame.display
pygame.display.init()

Peeking a GTK event; gtk.gdk.event_peek always returns None

I need to check whether the Escape key has been pressed during execution of some non-GUI code. (The code is in Python, but can easily call into C if necessary.) The code received a function from the GUI that it occasionally calls to check whether it has been interrupted. The question is how to implement this check.
By looking at the documentation, gdk_event_peek seems like an excellent choice for this:
def _check_esc(self):
event = gtk.gdk.event_peek()
if event is None or event.type not in (gtk.gdk.KEY_PRESS, gtk.gdk.KEY_RELEASE):
return False
return gtk.gdk.keyval_name(event.keyval) == 'Escape'
This doesn't work, however: the event returned from gtk.gdk.event_peek() is always None when the main loop is not running. Changing it to gtk.gdk.display_get_default().peek_event() doesn't help either. I assume the events are in the X event queue and are not yet moved to the GDK event queue. The documentation says:
Note that this function will not get more events from the windowing
system. It only checks the events that have already been moved to the
GDK event queue.
So, how does one transfer the event to the GDK event queue or? In other words, when does gtk.gdk.peek_event() ever return an event? Calling gtk.events_pending() doesn't have any effect.
Here is a minimal program to test it:
import gtk, gobject
import time
def code(check):
while 1:
time.sleep(.1)
if check():
print 'interrupted'
return
def _check_esc():
event = gtk.gdk.event_peek()
print 'event:', event
if event is None or event.type not in (gtk.gdk.KEY_PRESS, gtk.gdk.KEY_RELEASE):
return False
return gtk.gdk.keyval_name(event.keyval) == 'Escape'
def runner():
code(_check_esc)
gtk.main_quit()
w = gtk.Window()
w.show()
gobject.idle_add(runner)
gtk.main()
When running the code, the event printed is always None, even if you press Escape or move the mouse.
I also considered installing a handler for Escape and having the checker process events with the while gtk.events_pending(): gtk.main_iteration() idiom. This results in unqueuing and dispatch of all pending events, including keyboard and mouse events. The effect is that the GUI is responsive enabled while the code runs, which doesn't look well and can severely interfere with the execution of the code. The only event processed during execution should be the escape key to interrupt it.
I came up with a runner implementation that satisfies the criteria put forward in the question:
def runner():
# _check_esc searches for Escape in our queue
def _check_esc():
oldpos = len(queue)
while gtk.events_pending():
gtk.main_iteration()
new = itertools.islice(queue, oldpos, None)
return any(event.type == gtk.gdk.KEY_PRESS \
and gtk.gdk.keyval_name(event.keyval) == 'Escape'
for event in new)
queue = []
# temporarily set the global event handler to queue
# the events
gtk.gdk.event_handler_set(queue.append)
try:
code(_check_esc)
finally:
# restore the handler and replay the events
handler = gtk.main_do_event
gtk.gdk.event_handler_set(gtk.main_do_event)
for event in queue:
handler(event)
gtk.main_quit()
Compared to a peek-based solution, its advantage is that it handles the case when another event arrives after the keypress. The disadvantage is that it requires fiddling with the global event handler.

PyGame code doesn't execute properly when event is executed

I am trying to make the most simple pythong code that will respond when a button is pressed on a joystick. I used code from several different examples and I still cannot get it to work. The following code will not dispatch the event when I press the trigger (or any button for that matter)
import pygame
joy = []
def handleJoyEvent(e):
if e.type == pygame.JOYBUTTONDOWN:
str = "Button: %d" % (e.dict['button'])
if (e.dict['button'] == 0):
print ("Pressed!\n")
else:
pass
def joystickControl():
while True:
e = pygame.event.wait()
if (e.type == pygame.JOYBUTTONDOWN):
handleJoyEvent(e)
# main method
def main():
pygame.joystick.init()
pygame.display.init()
for i in range(pygame.joystick.get_count()):
myjoy = pygame.joystick.Joystick(i)
myjoy.init()
joy.append(myjoy)
# run joystick listener loop
joystickControl()
# allow use as a module or standalone script
if __name__ == "__main__":
main()
I assume you've tried leaving off the if and just printing str?
Your joystick might also not be working properly. Does it work in other programs?
If you are using linux you might need to install a joystick driver. For Windows, check the device manager.

Categories