码迷,mamicode.com
首页 > 编程语言 > 详细

单线程

时间:2015-11-05 20:40:03      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

# -*- 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
‘‘‘

 

单线程

标签:

原文地址:http://www.cnblogs.com/dengyg200891/p/4940501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!