Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whats wrong with my thread?
#5
A minimal example with a class without any queues, using locks instead.
With queues is a better approach, but I'm to tired to wrap my mind around queues.

import time
import threading

class Foo:
    def __init__(self, on_duration, off_duration):
        self._on_duration = on_duration
        self._off_duration = off_duration
        self.lock = threading.Lock()
        
    @property
    def off_duration(self):
        return self._off_duration
    
    @off_duration.setter
    def off_duration(self, value):
        with self.lock: # BLOCKING
            # in this block no other thread
            # is allowed to aquire the lock
            self._off_duration = value
    @property
    def on_duration(self):
        return self._on_duration
    
    @on_duration.setter
    def on_duration(self, value):
        with self.lock: # BLOCKING
            self._on_duration = value
            # in this block no other thread
            # is allowed to aquire the lock
            
    def start(self):
        thread = threading.Thread(target=self.run)
        thread.setDaemon(True)
        thread.start()
        
    def run(self):
        while True:
            with self.lock: # BLOCKING
                # here we assign the two values from
                # self.on_duration and self.off_duration
                # during this time, the other lock will
                # block, also the other assigments, which
                # are realized with @porperty.setter
                # will block this lock here.
                # this will prevent race conditions
                on_duration = self.on_duration
                off_duration = self.off_duration
                # during this time all other locks with
                # using the same lock object have to wait
                # a assigment is very short, so this
                # will not affect your gui
            # outside the lock block
            # here we use the new variables, we have
            # assigned before
            # no blocking here
            # and the sleep function won't block
            # because it's outside of the lock
            print('On', on_duration)
            time.sleep(on_duration)
            print('Off', off_duration)
            time.sleep(on_duration)

foo = Foo(5, 5)
foo.start()
time.sleep(10)
foo.on_duration = 2
foo.off_duration = 2
time.sleep(10)
# program ends here, main thread is killd
# because there are no non-daemon threads left
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Whats wrong with my thread? - by tony1812 - Aug-01-2017, 01:03 AM
RE: Whats wrong with my thread? - by Larz60+ - Aug-01-2017, 03:08 AM
RE: Whats wrong with my thread? - by tony1812 - Aug-01-2017, 03:15 PM
RE: Whats wrong with my thread? - by Larz60+ - Aug-01-2017, 06:09 PM
RE: Whats wrong with my thread? - by DeaD_EyE - Aug-01-2017, 10:14 PM
RE: Whats wrong with my thread? - by tony1812 - Aug-02-2017, 10:30 PM
RE: Whats wrong with my thread? - by DeaD_EyE - Aug-03-2017, 08:25 AM
RE: Whats wrong with my thread? - by tony1812 - Aug-04-2017, 01:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,815 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Whats wrong with the elif? inunanimous93 3 2,574 Nov-30-2020, 03:58 AM
Last Post: deanhystad
  Whats Wrong!? rjay81 3 2,335 May-13-2020, 08:02 PM
Last Post: rjay81
  Can u see this code and tell whats wrong with it? AhmadKamal 14 5,587 Apr-29-2020, 11:09 AM
Last Post: jefsummers
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,837 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  python gives wrong string length and wrong character thienson30 2 3,107 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Whats a good design/approach? hshivaraj 1 1,851 Sep-16-2019, 01:44 AM
Last Post: snippsat
  elevator simulator...whats the wrong at this code? tasos710 5 6,022 Jun-11-2019, 01:38 AM
Last Post: micseydel
  whats the difference between sys.exit() and break? mitmit293 1 4,183 Jan-27-2019, 09:46 PM
Last Post: ichabod801
  Whats wrong with this code? student1 1 2,503 May-18-2018, 04:19 PM
Last Post: skorpius_

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020