Created
August 1, 2014 18:59
-
-
Save osiloke/a3a33bfee4d3a0a3694b to your computer and use it in GitHub Desktop.
This highlights a memory leak i encountered in kivy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from kivy.core.window import Window | |
from kivy.graphics.context_instructions import Color | |
from kivy.uix.floatlayout import FloatLayout | |
from kivy.lang import Builder | |
from kivy import Logger | |
import gc | |
from kivy.app import App | |
from kivy.cache import Cache | |
from kivy.clock import Clock | |
from kivy.graphics.vertex_instructions import Rectangle | |
from kivy.properties import ObjectProperty | |
playlist = [] | |
for i in range(0, 1000): | |
playlist.append( | |
dict(path="http://dummy/image%s.jpg" % i, | |
name="Image %s" % i), | |
) | |
playlist.append( | |
dict(path="http://dummy/second_image%s.png" % i, | |
name="Second Image %s" % i), | |
) | |
Logger.debug("Signage: Playlist contains %s items" % len(playlist)) | |
class Player(FloatLayout): | |
rectangle = ObjectProperty(allownone=True) | |
playlist = ObjectProperty(playlist) | |
iter = None | |
def __init__(self, **kwargs): | |
super(Player, self).__init__(**kwargs) | |
with self.canvas: | |
Color(1, 0, 0) | |
self.rectangle = Rectangle(size=self.size, pos=self.pos) | |
self.iter = self.playlist.__iter__() | |
Clock.schedule_once(self.switch_item) | |
def switch_item(self, *args): | |
try: | |
item = self.iter.next() | |
except StopIteration: | |
pass | |
else: | |
#This just gets a path to the folder where images are stored | |
self.rectangle.source = "%s/%s" % (".", item["path"].split("/")[-1]) | |
Cache.print_usage() | |
Clock.schedule_once(self.switch_item, 0.) | |
# Define layout in dict form | |
layout = ''' | |
''' | |
Builder.load_string(layout) | |
class Test(App): | |
def build(self): | |
return Player(size=Window.size) | |
if __name__ == "__main__": | |
Test().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment