Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6221215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:05:39+00:00 2026-05-24T08:05:39+00:00

I have a pyglet window that has a attribute observer. The observer has a

  • 0

I have a pyglet window that has a attribute “observer”. The observer has a dictionary “dict”. In the main_loop() function the window re-draws the window according to the content of observer.dict. The observer itself is a thread reading a stream, adding readings to dict. The observer also has a timer thread that checks each second if there are obsolete items in dict, and, if so, deletes them.

Obviously, that might cause problems if items are added or deleted while the windows iterates over dict. My current workaround is to make each time a deep copy of the dict, and draw the copy. It seems to work, but it is a ugly solution.

I’m quite new to python and particularly w.r.t Threading / Locking / etc. I guess I need the observer to “lock” the dict when adding or deleting items. Can someone give my some hints where to look first?

I’ve tried to extract a meaningful structrue of my code, all would be too long. I hope you can get the main idea.

class GraphConsole(window.Window):
    def __init__(self, *args, **kwargs):
        window.Window.__init__(self, *args, **kwargs)

    def init(self, observer):
        self.observer = observer


    def main_loop(self):
        while not self.has_exit:
            ...
            self.draw()

    def draw(self):
        dict_copy = deepcopy(self.observer.dict) # <-- UGLY WORKAROUND
        for k, v in dict_copy.iteritems():
           ...


class Observer(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.dict = {}
        self.timer = Timer(1, self.delete_obsolete);
        self.timer.start()

    def run(self):
        while True:
        ...
         # read a stream
         self.dict.append(<new_element>)
         ...


    def delete_obsolete(self):
         ...
         del self.dict[...]
         ...



class Timer(threading.Thread):
    def __init__(self, interval_in_seconds, func):
         threading.Thread.__init__(self)
         self.interval_in_seconds = interval_in_seconds
         self.func = func

    def run(self):
         while True:
         self.func();
         time.sleep(self.interval_in_seconds)



if __name__ == "__main__":

    observer = Observer();
    observer.start()

    graph_console = GraphConsole()
    graph_console.init(observer)
    graph_console.main_loop()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:05:40+00:00Added an answer on May 24, 2026 at 8:05 am

    Probably some simple lock can fix your problem. Observer class:

    class Observer(threading.Thread):
        def __init__(self, lock):
            threading.Thread.__init__(self)
            self.dict_lock = lockthreading.RLock()
            self.dict = {}
            self.timer = Timer(1, self.delete_obsolete);
            self.timer.start()
    
        def run(self):
            while True:
            ...
             with self._dict_lock:
                 # read a stream
                 self.dict.append(<new_element>)
             ...
    
    
        def delete_obsolete(self):
             ...
             with self._dict_lock:
                 del self.dict[...]
             ...
    

    GraphConsole class:

    class GraphConsole(window.Window):
        def __init__(self, *args, **kwargs):
            window.Window.__init__(self, *args, **kwargs)
    
        def init(self, observer):
            self.observer = observer
    
        def main_loop(self):
            while not self.has_exit:
                ...
                self.draw()
    
        def draw(self):
            with self.observer.dict_lock:
                for k, v in dict_copy.iteritems():
               ...
    

    My initial answer was a bit incomplete, but I see you got the idea 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a GUI app in Pyglet that has to display tens to
I have some code that creates a batch using the pyglet library. The whole
Have created simple Ajax enabled contact forms before that have around 12 fields -
Have a LinqtoSql query that I now want to precompile. var unorderedc = from
Have some data in a sybase image type column that I want to use
I'm new to pyglet and i have a problem with video.. I'm trying to
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
Have some dates in my local Oracle 11g database that are in this format:
I am making a game in Python with Pyglet. I have just finished the
Have an app that can use tts to read text messages. It can also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.