python基础4(线程)

1. 时间戳,通常是一个字符序列,唯一地标识某一刻的时间。time模块中,time.time(),返回当前时间的时间戳。

import time

print('time.time():%f'  %time.time()) #注意要使用%f,表示浮点数
print (time.localtime( time.time() )) #把时间戳浮点数传递给localtime函数,将时间戳转换成目前的时间元组
print (time.asctime( time.localtime(time.time())) )#将目前的时间元组传给asctime函数,变成格式化时间
print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))#将年月日时间通过设定的格式显示出来
time.time():1522228807.167544
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=28, tm_hour=17, tm_min=20, tm_sec=7, tm_wday=2, tm_yday=87, tm_isdst=0)
Wed Mar 28 17:20:07 2018
2018-03-28 17:20:07

2. random模块中random()函数,在[0,1]范围内,随机生成一个实数。
import random
print(random.random())
0.41228321830766157



3.  time.sleep()函数推迟调用线程的运行,通过参数指定时间,表示进程挂起的时间
 
 
import time print( 'start: %s'%time.ctime())time.sleep( 6) print( 'end: %s'%time.ctime())
start: Wed Mar 28 17:32:43 2018end: Wed Mar 28 17:32:49 2018
4. 多任务可以由多进程完成,也可以由一个进程里面多个线程完成。 进程由若干个线程组成,一个进程由至少一个线程组成。

线程是操作系统直接支持的执行单元。任何进程会默认启动一个线程,该线程就是主线程,主线程又可以启动新的线程。threading模块的current_thread()函数,可以返回当前线程的实例。threading模块中的Thread()函数生成一个子线程。注意:python会自动给子线程命名为Thread-1,Thread-2....

5.

 

import time,threading #导入time模块和threading模块def loop(): print('thread %s is running...'% threading.current_thread().name) # loop线程的名字是loopthread n=0 while n<5: n+=1 print('thread %s, %s'% (threading.current_thread().name,n)) #注意:%后的参数要用括号括起来 time.sleep(1) #休眠1秒 print('thread %s ended.'% threading.current_thread().name)print('thread %s is running...'%threading.current_thread().name) #主线程的名字是MainThreadt=threading.Thread(target=loop,name='loopthread')#如果不定义name,python会自动命名Thread-1t.start() #线程开始跑t.join()print('thread %s ended.'% threading.current_thread().name)


thread MainThread is running...
thread loopthread is running...
thread loopthread, 1
thread loopthread, 2
thread loopthread, 3
thread loopthread, 4
thread loopthread, 5
thread loopthread ended.
thread MainThread ended.
 
 

6.Lock
多线程和多进程最大的不同在于,多进程中,同一个变量,各自有一份拷贝存在每个进程中,互不影响,而多线程中所有变量都由所有线程共享,所以,任何一个变量都可以被任何一个线程修改,为了避免,使用threading模块中的Lock()锁定变量,使该变量一次只能被一个线程使用。

import time, threading

balance = 0
lock=threading.Lock()
def change_it(n):
    # 先存后取,结果应该为0:
    global balance
    balance = balance + n
    balance = balance - n

def run_thread(n):
    for i in range(100000):
        #获取锁
        lock.acquire()
        try:
            #锁定n只能由一个线程使用后,可以避免出现n被多线程使用修改
           change_it(n)
        finally:
            #用完后要解锁
            lock.release()

t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)
0


7. 每个线程使用自己的局部变量,避免使用全局变量,从而避免影响其它线程,也不用像上边一样使用lock加锁
 
 
import threading # 创建全局ThreadLocal对象: local_school = threading.local() def process_student(): # 获取当前线程关联的student: std = local_school.student #为什么要这么麻烦把local_school绕进来呢,而不直接std=name呢,就是为了避免两个线程的参数混肴使用 print( 'Hello, %s (in %s)' % (std, threading.current_thread().name)) def process_thread(name): # 绑定ThreadLocal的student: local_school.student = name process_student()
#创建一个线程,target指向一个函数,args为该函数参数,name为这个线程的名字 t1 = threading.Thread( target= process_thread, args=( 'Alice',), name= 'Thread_A')
t2 = threading.Thread( target= process_thread, args=( 'Bob',), name= 'Thread_B') t1.start() #t1线程开始 t2.start() t1.join() #t1线程跑完结束 t2.join()

Hello, Alice (in Thread_A)
Hello, Bob (in Thread_B)



8. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值