# -*- coding: utf-8 -*- #python 27 #xiaodeng #线程和进程 #http://www.cnblogs.com/fnng/p/3670789.html #单线程时代: #这里其实是2个进程,两者之间没有关联的2个进程,分别按照先后顺序在运行 from time import sleep,ctime def music(): for i in range(2): print ‘i was listening to music. %s‘%ctime() sleep(1) def move(): for i in range(2): print ‘i was at the movies! %s‘%ctime() sleep(5) if __name__==‘__main__‘: music()#进程1 move()#进程2 print ‘All over %s‘%ctime()#输出为:Thu Nov 05 12:01:06 2015 ‘‘‘ i was listening to music. Thu Nov 05 12:00:54 2015 i was listening to music. Thu Nov 05 12:00:55 2015 i was at the movies! Thu Nov 05 12:00:56 2015 i was at the movies! Thu Nov 05 12:01:01 2015 All over Thu Nov 05 12:01:06 2015 ‘‘‘ print ‘##‘*20 #music()和move()更应该被看作是音乐和视频播放器,至于要播放什么歌曲和视频应该由我们使用时决定 #对上面的代码进行改造~~ import threading from time import ctime,sleep def music(func): for i in range(2): print ‘listening to %s. %s‘%(func,ctime()) sleep(1) def move(func): for i in range(2): print ‘i love look %s.%s‘%(func,ctime()) sleep(5) if __name__==‘__main__‘: music(u‘大悲咒‘) move(u‘大秦帝国‘)#这里u在前面,这个变量就是unicode编码,不用解码。 print ‘over:%s‘%ctime() ‘‘‘ listening to 大悲咒. Thu Nov 05 12:17:13 2015 listening to 大悲咒. Thu Nov 05 12:17:14 2015 i love look 大秦帝国.Thu Nov 05 12:17:15 2015 i love look 大秦帝国.Thu Nov 05 12:17:20 2015 over:Thu Nov 05 12:17:25 2015 ‘‘‘
时间: 2024-10-11 17:20:09