1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| import logging import random import sys import time
from multiprocessing import Process from multiprocessing import Queue
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format='%(message)s')
MY_STATUS = Queue() MIND_OUT = logging.getLogger(__name__)
class Mind(object): def __init__(self, who_am_i): self.who_am_i = who_am_i self.alive = True
def look_until_i_die(self): while self.alive: self.think(self.get_self_status())
def get_self_status(self): return MY_STATUS.get()
def think(self, self_status): MIND_OUT.info('''Oh I'm {}.'''.format(self_status)) if self_status == 'cycling': self.what_should_I_think_when_cycling() else: self.todo()
def what_should_I_think_when_cycling(self): self.what_should_I_think_when_cycling()
def todo(self): MIND_OUT.info('Oh maybe I need a break.') time.sleep(random.choice(xrange(0, 10)))
def main(): my_mind = Mind('Pike.SZ.fish') p = Process(target=my_mind.look_until_i_die) p.start() while my_mind.alive: MY_STATUS.put(random.choice(['eating', 'coding', 'sleeping', 'walking', 'running', 'thinking', 'jumping', 'laughing', 'cycling'])) time.sleep(random.choice(xrange(0, 10)))
if __name__ == '__main__': main()
|