Programming Robots Python-Threading. Programming Robots Thread vs Threading Python offers two thread modules  Thread: used up to now; including this.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Ch 7 B.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Practice Session 7 Synchronization Liveness Guarded Methods Model
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
Multithreaded Programming in Python Nick Anastasia.
Programming Robot Python Classes. Programming Robot Python Classes Require Politeness: Python does not have the privacy mechanisms of C++ and Java and.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Programming Robots Musical Chairs. Programming Robots The Game: Version 1:  Wander around the corral until a bright light shines.  Upon seeing the bright.
1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);
Software Engineering Oct-01 #11: All About Threads Phil Gross.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
28-Jun-15 Producer-Consumer An example of using Threads.
Programming Robots Programming Notes:. Programming Robots Programming a Timed Event Suppose we want a behaviour like the default behaviour to go on but.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Fundamentals of Python: From First Programs Through Data Structures
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
Threads. Sequential control Sequential programs: begin execute end At any time, there is a single point of execution Threads: also structured as begin.
 socket.socket() returns socket object: _socketobject  Most of socket API are methods on socket objects or functions  Value result arguments (e.g.,
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Threading Eriq Muhammad Adams J
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Sistem Operasi © Sekolah Tinggi Teknik Surabaya 1.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Multi-Threading in Java
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Tutorial 3: Homework 2 and Project 2
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Java Thread Programming
Tutorial 2: Homework 1 and Project 1
SCJP 9/10 Threads.
Multithreading / Concurrency
Threaded Programming in Python
Multi Threading.
Multithreaded Programming in Java
Threads Chate Patanothai.
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Structured Programming Taken from notes by Dr. Neil Moore
Multithreading.
Threaded Programming in Python
21 Threads.
Computer Science 2 06A-Java Multithreading
NETWORK PROGRAMMING CNET 441
CS333 Intro to Operating Systems
Software Engineering and Architecture
Implementing Variables in Your Programs
More concurrency issues
Presentation transcript:

Programming Robots Python-Threading

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()‏

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

Programming Robots Whirling Dervishes

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().

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()‏

Programming Robots Code 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)‏ 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()‏

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

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]

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()‏

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.