Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The Application Event Loop
2.1.
Customization of the Application Event loop
2.2.
EventLoop Events
3.
The Default Idle Policy
4.
Manual Dispatching of Events
4.1.
Drawbacks of Manual Dispatching of Events
5.
Frequently Asked Questions
5.1.
Explain the schedule_once function.
5.2.
Name some attributes of the color buffer.
5.3.
Share some insights on pyglet.extlibs.
5.4.
Do you know anything about tearing?
6.
Conclusion
Last Updated: Mar 27, 2024

The Application Event Loop in Pyglet

Author Rupal Saluja
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this blog, we will learn about the Application Event Loop in Pyglet. Pyglet which is intended for game development is a pure-python-based cross-platform application framework. It supports loading images and videos, playing sounds and music, windowing, OpenGL graphics, user interface event handling, and whatnot. Besides, it works on the most popular Operating Systems, that is, Windows, Mac OS X, and Linux.

The Application Event Loop

In Pyglet, it is necessary that applications enter an application event loop to process operating system events such as a mouse or keyboard events. This application event loop will check for new events continuously, will dispatch those events, and will update the contents of all open windows.

In the case of Pyglet, it provides an application event loop that is tuned for great performance and low power usage on the most popular Operating Systems. We just need to call the function below to enter the event loop after creating their initial set of windows and attaching event handlers.

pyglet.app.run()

 

This function will not return until all the open windows have been closed or until exit() has been called.

Customization of the Application Event loop

The ‘EventLoop’ class is the one in which the pyglet application event loop is encapsulated. It provides several hooks that can be overridden for customizing the behavior of the Application Event loop. Since typical applications and games are unlikely to require this functionality, thus, it is recommended for advanced users only.

In case there is a need to use the ‘EventLoop’ directly, you need to instantiate it first and then call run. Refer to the following piece of code to do so. Note that only one ‘EventLoop’ can run at a time.

event_loop = pyglet.app.EventLoop()
event_loop.run()

EventLoop Events

The most popular of the several events of the event loop is on_window_close(). This event comes into instance whenever a window is being closed. If there are no more windows, the default handler for this event exits the event loop. To override this behavior to exit the application whenever any window is closed, refer to the following lines of code. 

event_override = pyglet.app.EventLoop()
@event_override.event
def not_close_window(window):
        event_override.exit()
        return pyglet.event.EVENT_HANDLED
event_override.run()
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

The Default Idle Policy

To call scheduled clock functions, redraw windows, and decide how idle the application is the pyglet.app.EventLoop.idle() method is called for every iteration of the event loop. In case it uses many windows, then for tuning the performance of the application, this function can be overridden.

You should take note that during every user event, this default policy causes every window to be redrawn. If you have knowledge of the effects produced by each event on a particular window, you can surely improve the performance of this method.

Manual Dispatching of Events

Certain other windowing toolkits such as PyGame & SDL and even the earlier versions of Pyglet required that the application developer write their own event loop. This is usually looked at as an inconvenience when compared to the automatic creation of Events. Yet, this can be necessary for some circumstances when there is a need to combine pyglet with other toolkits. To understand how you can dispatch events manually, refer to the following piece of code.

while True:
       pyglet.clock.tick()
       
       for watch in pyglet.app.windows:
       watch.switch_to()
       watch.dispatch_events()
       watch.dispatch_event(‘on_draw’)
       watch.flip()

 

You can see we have used various functions in the above piece of code, such as switch_to()dispatch_events()dispatch_event(on_draw) and flip(). To know why these methods are being used, refer to the table below.

METHODS

THEIR USE

pyglet.clock.tick() The method is called to ensure that the scheduled functions are being called, including the internal data pump functions for playing sounds, animations or video.
switch_to() This method prepares the window to be ready and switches constantly for the upcoming events.
dispatch_events()

This method checks the window’s operating system event queue for user input. Also, dispatches, if any events are found. 

This method doesn’t wait for any input. In case there is no input, i.e., there are no events pending, control is returned to the program immediately.

flip() As the name suggests, this method facilitates and smoothens the dispatch of events.

Drawbacks of Manual Dispatching of Events

Though we know that it is possible to write our own event loop, certain drawbacks mentioned below are the reasons manual dispatching is strongly discarded.

  • It would be really difficult to write a manual event loop that will not occupy 100% CPU. At the same time, the event loop should be responsive to the user’s input.

 

  • EventLoop is more responsive to user events. It continues calling clock functions even when windows are being resized because it is tuned for certain specific operating systems. Also, there will be a continuous track of the menu bar. To produce these all manually seems pretty difficult.

 

  • Plenty of in-built functions for most toolkits are provided by the EventLoop class itself. Therefore, it is preferred not to use manual dispatching.

Frequently Asked Questions

Explain the schedule_once function.

The schedule_once function is defined in pyglet.clock. Its two parameters are func (function) and delay (float). The func parameter is called when the timer lapses and the delay parameter signifies the number of seconds to wait before the timer elapses. 

Name some attributes of the color buffer.

Some attributes of the color buffer are-

  • buffer_size
  • red_size, blue_size, green_size, alpha_size
  • sample_buffers and samples
  • stereo
  • double_buffer

 

Share some insights on pyglet.extlibs.

The pyglet.extlibs are the external dependencies for Pyglet. To publish Pyglet as a fully self-contained package, these dependencies are included.

Do you know anything about tearing?

Double buffering eliminates one cause of flickering and introduces another source of flicker, known as tearing.

Tearing becomes visible when we display fast-moving objects and produce an animation. While the video display is still in the process of reading data from the frame buffer, which causes the top half of the display to show the previous frame while the bottom half shows the updated frame, a buffer flip occurs.

Conclusion

In this article, we have extensively discussed the Application Event loop in Pyglet, we have also discussed its customization, its idle policy, and its manual dispatching, in detail.

We hope that this blog helped you increment your knowledge regarding every aspect of the Application Event Loop. To know more about PygletPygame, and Basics of Python, click on the links. 

For peeps out there who want to enhance their knowledge about Data Structures, Algorithms, Power programming, JavaScript, or any other upskilling, please refer to guided paths on Coding Ninjas Studio. Enroll in our courses, go for mock tests and solve problems available and interview puzzles. Also, you can put your attention towards interview stuff- interview experiences and an interview bundle for placement preparations. 

Do upvote our blog to help other ninjas grow.

Happy Coding!

Previous article
OpenGL Interface in pyglet
Next article
Pyglet Options
Live masterclass