标签:time thread 解释 系统 char code selection 多次 rtl
from threading import Thread def func(n): print("线程的创建", n) t = Thread(target=func, args=(‘pontoon‘,)) t.start()
# 多线程并发 import os import time from threading import Thread from multiprocessing import Process def func(): time.sleep(2) print(‘hello‘, os.getpid()) if __name__ == ‘__main__‘: # for i in range(10): # p1 = Process(target=func) # p1.start() # print(‘主进程pid‘, os.getpid()) # print(‘-‘ * 30) for i in range(10): t1 = Thread(target=func) t1.start() print(‘主线程pid‘, os.getpid()) # 结果我就不贴出来了 # 两者并没有什么实质上的区别,当然也看的出一点点差异就是主线程执行(全部一起出结果)要比主进程快(5个5个来)
1 import os 2 from threading import Thread 3 from multiprocessing import Process 4 5 6 def func(): 7 print(‘hello‘, os.getpid()) 8 9 10 if __name__ == ‘__main__‘: 11 p1 = Process(target=func) 12 p2 = Process(target=func) 13 p1.start() 14 p2.start() 15 print(‘主进程pid‘, os.getpid()) 16 print(‘-‘ * 30) 17 18 t1 = Thread(target=func) 19 t2 = Thread(target=func) 20 t1.start() 21 t2.start() 22 print(‘主线程pid‘, os.getpid()) 23 24 # 注意执行顺序 程序从上往下执行,最后执行子进程 25 >>>主进程pid 155928 26 ------------------------------ 27 hello 155928 # 子线程 28 hello 155928 # 子线程 29 主线程pid 155928 30 hello 134120 # 子进程 31 hello 74396 # 子进程
1 # 开启效率的对比: 2 import os 3 import time 4 from threading import Thread 5 from multiprocessing import Process 6 7 8 def func(): 9 time.sleep(2) 10 print(‘hello‘, os.getpid()) 11 12 13 if __name__ == ‘__main__‘: 14 # start_time = time.time() 15 # for i in range(30): 16 # p1 = Process(target=func) 17 # p1.start() 18 # print(‘主进程pid‘, os.getpid()) 19 # end_time = time.time() 20 # print(‘并发进程所用时间:‘,end_time - start_time) 21 22 start_time = time.time() 23 for i in range(30): 24 t1 = Thread(target=func) 25 t1.start() 26 print(‘主线程pid‘, os.getpid()) 27 end_time = time.time() 28 print(‘并发线程所用时间:‘, end_time - start_time) 29 30 >>>并发线程所用时间: 0.006010770797729492 31 并发进程所用时间: 1.3949854373931885 32 # 差距相当大
1 # 3、线程之间数据的共享问题 2 3 import os 4 from threading import Thread 5 6 7 def func(): 8 global g 9 g = 44 10 print(g, os.getpid()) 11 12 13 g = 100 14 t_list = [] 15 for i in range(3): 16 t = Thread(target=func) 17 t.start() 18 t_list.append(t) 19 20 for i in t_list: t.join() 21 print(g) 22 23 >>>44 112740 24 44 112740 25 44 112740 26 44
1 import os 2 from threading import Thread 3 4 5 def func(n): 6 inp = input(‘%s:‘ % n) 7 print(inp) 8 9 10 for i in range(3): 11 t = Thread(target=func, args=(‘name%s‘ % i, )) 12 t.start() 13 14 >>>name0:name1:name2:fgh 15 fgh 16 # 有点懵逼
进程是最小的内存分配单位
线程是操作系统调度的最小单位
进程中至少含有一个线程
进程中可以开启多个线程
开启线程的时间要远小于进程
多个线程内部都有自己的数据栈,数据栈中的数据不共享
全局变量在多线程之间是共享的
多线程是可以使用Input的(IO请求并不会堵塞)
from threading import Thread, Lock import time def func(): global n temp = n time.sleep(1) n = temp - 1 n = 10 # 线程的数据是共享的 t_list = [] for i in range(10): t = Thread(target=func) t.start() t_list.append(t) for i in t_list: i.join() print(n) >>>9 # 得到的为什么会是一个9
1 def func(lock): 2 lock.acquire() 3 global n 4 temp = n 5 time.sleep(1) 6 n = temp - 1 7 lock.release() 8 9 10 n = 10 11 lock = Lock() 12 t_list = [] 13 for i in range(10): 14 t = Thread(target=func, args=(lock, )) 15 t.start() 16 t_list.append(t) 17 18 for i in t_list: 19 i.join() 20 21 >>>0
1 import time 2 from threading import Thread, Lock 3 4 noodle_lock = Lock() 5 fork_lock = Lock() 6 7 8 def eat1(name): 9 noodle_lock.acquire() 10 print(‘%s 抢到了面条‘%name) 11 fork_lock.acquire() 12 print(‘%s 抢到了叉子‘%name) 13 print(‘%s 吃面‘%name) 14 fork_lock.release() 15 noodle_lock.release() 16 17 18 def eat2(name): 19 fork_lock.acquire() 20 print(‘%s 抢到了叉子‘ % name) 21 time.sleep(1) 22 noodle_lock.acquire() 23 print(‘%s 抢到了面条‘ % name) 24 print(‘%s 吃面‘ % name) 25 noodle_lock.release() 26 fork_lock.release() 27 28 29 for name in [‘aaa‘, ‘bbb‘, ‘ccc‘]: 30 t1 = Thread(target=eat1, args=(name,)) 31 t2 = Thread(target=eat2, args=(name,)) 32 t1.start() 33 t2.start() 34 35 >>>aaa 抢到了面条 36 aaa 抢到了叉子 37 aaa 吃面 38 aaa 抢到了叉子 39 bbb 抢到了面条
1 import time 2 from threading import Thread, Rock 3 4 5 fork_lock = noodle_lock = RLock() # 注意这种写法 6 def eat1(name): 7 noodle_lock.acquire() 8 print(‘%s 抢到了面条‘%name) 9 fork_lock.acquire() 10 print(‘%s 抢到了叉子‘%name) 11 print(‘%s 吃面‘%name) 12 fork_lock.release() 13 noodle_lock.release() 14 15 16 def eat2(name): 17 fork_lock.acquire() 18 print(‘%s 抢到了叉子‘ % name) 19 time.sleep(1) 20 noodle_lock.acquire() 21 print(‘%s 抢到了面条‘ % name) 22 print(‘%s 吃面‘ % name) 23 noodle_lock.release() 24 fork_lock.release() 25 26 27 for name in [‘aaa‘, ‘bbb‘, ‘ccc‘]: 28 t1 = Thread(target=eat1, args=(name,)) 29 t2 = Thread(target=eat2, args=(name,)) 30 t1.start() 31 t2.start() 32 33 aaa 抢到了面条 34 aaa 抢到了叉子 35 aaa 吃面 36 aaa 抢到了叉子 37 aaa 抢到了面条 38 aaa 吃面 39 bbb 抢到了面条 40 bbb 抢到了叉子 41 bbb 吃面 42 bbb 抢到了叉子 43 bbb 抢到了面条 44 bbb 吃面 45 ccc 抢到了面条 46 ccc 抢到了叉子 47 ccc 吃面 48 ccc 抢到了叉子 49 ccc 抢到了面条 50 ccc 吃面
1 # 7、信号量 2 3 from threading import Thread,Semaphore 4 import threading 5 import time 6 7 8 def func(): 9 sm.acquire() 10 print(‘%s get sm‘ %threading.current_thread().getName()) 11 time.sleep(3) 12 sm.release() 13 14 15 if __name__ == ‘__main__‘: 16 sm=Semaphore(5) 17 for i in range(23): 18 t=Thread(target=func) 19 t.start()
1 import threading 2 import time,random 3 from threading import Thread,Event 4 5 def conn_mysql(): 6 count=1 7 while not event.is_set(): 8 if count > 3: 9 raise TimeoutError(‘链接超时‘) 10 print(‘<%s>第%s次尝试链接‘ % (threading.current_thread().getName(), count)) 11 event.wait(0.5) 12 count+=1 13 print(‘<%s>链接成功‘ %threading.current_thread().getName()) 14 15 16 def check_mysql(): 17 print(‘\033[45m[%s]正在检查mysql\033[0m‘ % threading.current_thread().getName()) 18 time.sleep(random.randint(2,4)) 19 event.set() 20 if __name__ == ‘__main__‘: 21 event=Event() 22 conn1=Thread(target=conn_mysql) 23 conn2=Thread(target=conn_mysql) 24 check=Thread(target=check_mysql) 25 26 conn1.start() 27 conn2.start() 28 check.start()
1 from threading import Condition, Thread 2 3 4 def func(con, i): 5 con.acquire() 6 con.wait() 7 print(‘在第{0}个循环里‘.format(i)) 8 con.release() 9 10 11 con = Condition() 12 for i in range(10): 13 Thread(target=func, args=(con, i)).start() 14 while True: 15 num = int(input(‘>>>‘)) 16 con.acquire() 17 con.notify(num) # 在钥匙 18 con.release()
1 import time 2 from concurrent.futures import ThreadPoolExecutor 3 4 5 def func(n): 6 time.sleep(2) 7 print(n) 8 return n * n 9 10 11 t_pool = ThreadPoolExecutor(max_workers=5) # 在线程池中创建5个线程 12 t_list = [] 13 for i in range(20): 14 t = t_pool.submit(func, i) # 创建子线程 15 t_list.append(t) 16 17 t_pool.shutdown() # close() + join() 方法 18 print(‘主线程‘) 19 for i in t_list: 20 print("***", t.result()) # 取return的结果
1 # 11、线程池中加回调函数 2 import time 3 from concurrent.futures import ThreadPoolExecutor 4 5 6 def func(n): 7 time.sleep(2) 8 print(n) 9 return n * n 10 11 12 def call_back(m): 13 print("结果是 %s" % m.result()) 14 15 16 t_pool = ThreadPoolExecutor(max_workers=5) # 17 for i in range(20): 18 t_pool.submit(func, i).add_done_callback(call_back) 19 20 >>>0 21 结果是 0 22 4 23 3 24 结果是 9 25 结果是 16 26 ...
标签:time thread 解释 系统 char code selection 多次 rtl
原文地址:https://www.cnblogs.com/pontoon/p/10449651.html