1 / 11

Python-Threading

Python-Threading. Thread vs Threading. Python offers two thread modules Thread : used up to now; including this homework. start_new_thread() : simple mechanism that hides many details of running athread Locking : Threading : Thread class which we subclass and override run().

jacqui
Download Presentation

Python-Threading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python-Threading Programming Robots

  2. Programming Robots Thread vs Threading • Python offers two thread modules • Thread: used up to now; including this homework. • start_new_thread(): simple mechanism that hides many details of running athread • Locking: • Threading: • Thread class which we subclass and override run(). vlock = thread.allocate_lock()‏ vlock.acquire()‏ ... vlock.release()‏

  3. Programming Robots Program Paradigm: • Create two robot objects. • Subclass Thread and implement run(). • Create two threads and start them • Wait in the main thread until the two child threads are done. • Avoid infinite loops that consume CPU cycles

  4. Programming Robots Whirling Dervishes

  5. Programming Robots Using Threading: • The key here is to avoid having the thread constantly consuming CPU cycles by checking if it is time to do its thing. • The threading.Event class will cause the thread that calls the threading.Event.wait() method to go to sleep. • It will stay asleep until it is awoken by another thread calling the threading.Event.set() method. • In order for the wait() method to do the correct thing in the future it is important to call threading.Event.clear().

  6. Programming Robots Robot Methods: Wait: This method invokes the Event.wait() method and always returns False. Dflt: This method determines direction to to turn and returns True calling doRotate()‏

  7. Programming Robots Code def main(): d1 = WhirlingDervish(0,0)‏ d2 = WhirlingDervish(0,0)‏ d1.addToProgramList('Wait')‏ d1.addToProgramList('dflt')‏ d2.addToProgramList('Wait')‏ d2.addToProgramList('dflt')‏ t1 = MyThread(d1)‏ t2 = MyThread(d2)‏ t1.start()‏ t2.start()‏ t1.join() # blocks until t1 done t2.join() # blocks until t2 done main()‏ from myRobot import * from WhirlingDervish import * import threading class MyThread (threading.Thread): def __init__(self,wd): threading.Thread.__init__(self)‏ self.myDerv = wd def run(self): self.myDev.main(120)‏

  8. Programming Robots More Code: from myro import * from myRobot import * from random import Random import threading class WhirlingDervish (MyScribbler): myEvent = threading.Event()‏ myLock = threading.Lock()‏ direction = 1 def __init__(self,port,t=0,r=0): MyScribbler.__init__(self,port,t,r)‏ # add additional behaviours here for this program only self.masterMethodsList['Wait'] = self.Wait self.masterMethodsList['dflt'] = self.dflt

  9. Programming Robots def Wait(self): # instead of consuming CPU time this dervish goes to sleep print 'Waiting' WhirlingDervish.myEvent.wait()‏ # upon waking up; it fails through to the default method return [False,self.T,self.R,self.move] def dflt(self): rotate = +1 if (WhirlingDervish.direction == -1): rotate = -1 # dflt always succeeds return [True,0,rotate,self.doRotate]

  10. Programming Robots More Code def doRotate(self,t,r): global direction self.move(t,r)‏ sleep(Random.random()*4.0)‏ self.stop()‏ WhirlingDervish.myLock.acquire()‏ if (Random.random() < 0.5) : WhirlingDervish.direction = 1 else: WhirlingDervish.direction = -1 WhirlingDervish.myLock.release()‏ WhirlingDervish.myEvent.set()‏ WhirlingDervish.myEvent.clear()‏

  11. Programming Robots Other threading Clases • Event.set() wakes up all threads waiting on the same event. • We work at a lower level – threading.Condition. • In threading.Condition we use two methods – notify() and notifyAll(). NotifyAll() is just Event.set(). • Notify() tells the thread manager to awaken one waiting thread, we don't know which.

More Related