[关闭]
@linux1s1s 2017-08-09T10:35:49.000000Z 字数 1278 阅读 783

Python 核心编程笔记三

Python 2017-08


这里简要记录 Python核心编程 读书笔记 整段代码 可以直接运行

CODE-主线程

  1. # -*- coding:gb18030 -*-
  2. # 主线程
  3. from time import sleep, ctime
  4. def loop0():
  5. print 'start loop 0 at: ', ctime()
  6. sleep(4)
  7. print 'stop loop 0 at: ', ctime()
  8. def loop1():
  9. print 'start loop 1 at: ', ctime()
  10. sleep(2)
  11. print 'stop loop 1 at: ', ctime()
  12. def main():
  13. print 'Starting at: ', ctime()
  14. loop0()
  15. loop1()
  16. print 'Stop at: ', ctime()
  17. if __name__ == '__main__':
  18. main()

RESULT-主线程

  1. C:\Python27\python.exe H:/workspace/python-hw/hw-3.py
  2. Starting at: Wed Aug 09 18:35:47 2017
  3. start loop 0 at: Wed Aug 09 18:35:47 2017
  4. stop loop 0 at: Wed Aug 09 18:35:51 2017
  5. start loop 1 at: Wed Aug 09 18:35:51 2017
  6. stop loop 1 at: Wed Aug 09 18:35:53 2017
  7. Stop at: Wed Aug 09 18:35:53 2017
  8. Process finished with exit code 0

CODE-多线程

  1. # -*- coding:gb18030 -*-
  2. import thread
  3. from time import sleep, ctime
  4. def loop0():
  5. print 'Starting loop 0 at: ', ctime()
  6. sleep(4)
  7. print 'Stop loop 0 at: ', ctime()
  8. def loop1():
  9. print 'Starting loop 1 at: ', ctime()
  10. sleep(2)
  11. print 'Stop loop 1 at: ', ctime()
  12. def main():
  13. print 'Starting at: ', ctime()
  14. thread.start_new_thread(loop0, ())
  15. thread.start_new_thread(loop1, ())
  16. sleep(6)
  17. print 'Stop at: ', ctime()
  18. if __name__ == '__main__':
  19. main()

RESULT-多线程

  1. C:\Python27\python.exe H:/workspace/python-hw/hw-4.py
  2. Starting at: Wed Aug 09 18:36:09 2017
  3. Starting loop 0 at: Wed Aug 09 18:36:09 2017
  4. Starting loop 1 at: Wed Aug 09 18:36:09 2017
  5. Stop loop 1 at: Wed Aug 09 18:36:11 2017
  6. Stop loop 0 at: Wed Aug 09 18:36:13 2017
  7. Stop at: Wed Aug 09 18:36:15 2017
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注