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

python多线程

时间:2019-04-09 18:24:30      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:.com   eve   arm   charm   []   tac   tcl   student   CND   

多线程代码:

技术图片
  1 #!/usr/bin/env python3
  2 # -*- coding: utf-8 -*-
  3 # @Time    : 2019/3/28 10:49
  4 # @Author  : suwy
  5 # @Email   : suwy@cndatacom.com
  6 # @File    : multithread_demo.py
  7 # @Software: PyCharm
  8 
  9 import threading
 10 import time
 11 import queue
 12 
 13 
 14 flag = 1
 15 #通过类继承实现多线程
 16 class myThread(threading.Thread):
 17     def __init__(self,threadid,name,counter,threadlock,q):
 18         threading.Thread.__init__(self)
 19         self.threadid = threadid
 20         self.name = name
 21         self.counter = counter
 22         self.threadlock = threadlock
 23         self.q = q
 24 
 25     def run(self):
 26         print("开始线程:" + self.name)
 27         #获取锁
 28         # self.threadlock.acquire()
 29         # print_time(self.name,self.counter,5)
 30         #释放锁
 31         # self.threadlock.release()
 32         print_data(self.name,self.q,self.threadlock)
 33         print("线程结束:" + self.name)
 34 
 35 def print_time(threadName,delay,counter):
 36     while counter:
 37         time.sleep(delay)
 38         print("%s: %s" % (threadName, time.ctime(time.time())))
 39         counter -= 1
 40 
 41 def print_data(threadName,q,threadlock):
 42     while  flag:
 43         threadlock.acquire()
 44         if not q.empty():
 45             data = q.get()
 46             threadlock.release()
 47 
 48             print("%s:%s" % (threadName,data))
 49         else:
 50             threadlock.release()
 51         time.sleep(1)
 52         print(flag)
 53 
 54 def testClassthread():
 55 
 56     # 创建新线程
 57     threadlock = threading.Lock()
 58     q = queue.Queue(10)
 59 
 60     thread1 = myThread(1, "Thread-1", 1,threadlock,q)
 61     thread2 = myThread(2, "Thread-2", 2,threadlock,q)
 62 
 63     # 开启新线程
 64     thread1.start()
 65     thread2.start()
 66     threads = []
 67     # 添加线程到线程列表
 68     threads.append(thread1)
 69     threads.append(thread2)
 70 
 71     threadlock.acquire()
 72     for i in range(10):
 73         q.put(i)
 74     threadlock.release()
 75     print(q.qsize())
 76     print(q.full())
 77     # print(q.task_done())
 78     while True:
 79         if q.empty():
 80             global  flag
 81             flag = 0
 82             break
 83         else:
 84             print(q.qsize())
 85             # print(q.task_done())
 86 
 87     # 等待所有线程完成
 88     for t in threads:
 89         t.join()
 90     print ("退出主线程")
 91 
 92 def fun(i,name):
 93     print("{}:{}".format(name,i))
 94 
 95 def testfunthread():
 96     threadlist = []
 97     for i in range(10):
 98         name = "thread-{}".format(i)
 99         t = threading.Thread(target=fun,args=(i,name))
100         t.start()
101         threadlist.append(t)
102     for t in threadlist:
103         t.join()
104 
105 
106 
107 
108 def change_it(n):
109     # 先存后取,结果应该为0:
110     global balance
111     balance = balance + n
112     balance = balance - n
113 
114 def run_thread(n):
115     for i in range(100000):
116         # change_it(n)
117         # 先要获取锁:
118         lock.acquire()
119         try:
120             # 放心地改吧:
121             change_it(n)
122         finally:
123             # 改完了一定要释放锁:
124             lock.release()
125 
126 def testchange():
127     global balance
128     global  lock
129     lock = threading.Lock()
130     balance = 0
131     t1 = threading.Thread(target=run_thread, args=(5,))
132     t2 = threading.Thread(target=run_thread, args=(8,))
133     t1.start()
134     t2.start()
135     t1.join()
136     t2.join()
137     print(balance)
138 
139 def process_student():
140     # 获取当前线程关联的student:
141     std = local_school.student
142     print(Hello, %s (in %s) % (std, threading.current_thread().name))
143 
144 def process_thread(name):
145     # 绑定ThreadLocal的student:
146     local_school.student = name
147     process_student()
148 
149 def testlocal():
150     global local_school
151     local_school = threading.local()
152     t1 = threading.Thread(target=process_thread, args=(Alice,), name=Thread-A)
153     t2 = threading.Thread(target=process_thread, args=(Bob,), name=Thread-B)
154     t1.start()
155     t2.start()
156     t1.join()
157     t2.join()
158 
159 if __name__ == __main__:
160     # testClassthread()
161     # testfunthread()
162     # testchange()
163     testlocal()
View Code

 

python多线程

标签:.com   eve   arm   charm   []   tac   tcl   student   CND   

原文地址:https://www.cnblogs.com/swyroc/p/10678524.html

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