# L-37 MCS 275 Wed 14 Apr 2010 : conversation.py # This script uses threads to simulate a conversation. # Guests arrive at a party, say a number of sentences, # and then leave. from randomwords import RandomSentence from threading import * import time import random class Guest(Thread): """ Shuffling a shared message by threads. """ def __init__(self,t,n): """ Initializes shuffler with name t, and number of sentences. """ Thread.__init__(self,name=t) print 'guest ' + t + ' is born' self.n = n def run(self): """ Guests arrive within 1 and 5 time units. After sleeping between 1 and 6 time units, they utter a random sentence. """ s = self.getName() time.sleep(random.randint(1,5)) print 'guest ' + s + ' arrives' for i in range(0,self.n): time.sleep(random.randint(1,6)) print s + ' says ' + RandomSentence(10,10) time.sleep(random.randint(1,5)) print 'guest ' + s + ' leaves' def main(): """ Defines the number of guests and starts the simulation. """ n = input('give #guests : ') G = [] k = ord('A') for i in range(0,n): G.append(Guest(chr(k+i),n)) print 'starting the conversation' for g in G: g.start() print 'conversation has started' if __name__ == "__main__": main()