随笔分类 -  进程线程网络

摘要:```python import threading a = b = 0 lock = threading.Lock() def value(): while True: lock.acquire() if a != b: print("a = %d,b = %d"%(a,b)) lock.release()... 阅读全文
posted @ 2018-11-09 20:11 IndustriousHe 阅读(208) 评论(0) 推荐(0)
摘要:```python from multiprocessing import Process,Lock import time,sys def worker1(stream): lock.acquire() # 加锁 for i in range(5): time.sleep(1) stream.write("Lock acquired via\n... 阅读全文
posted @ 2018-11-09 20:05 IndustriousHe 阅读(215) 评论(0) 推荐(0)
摘要:```python from multiprocessing import Queue #创建消息队列对象 q = Queue(3) i = 0 # 存放消息 while True: #判断队列是否满了 if q.full(): print("queue is full") break q.put("hello" + str(i)) ... 阅读全文
posted @ 2018-11-09 19:53 IndustriousHe 阅读(189) 评论(0) 推荐(0)
摘要:```python from multiprocessing import Process,Pipe import os,time #创建管道对象 #当参数为False的时候child只能recv parent只能send # child_conn,parent_conn = Pipe(False) child_conn,parent_conn = Pipe() #子进程函数 def fu... 阅读全文
posted @ 2018-11-09 19:48 IndustriousHe 阅读(201) 评论(0) 推荐(0)
摘要:```python import multiprocessing as mp from time import sleep import os def worker(msg): sleep(2) print(msg) return "worker return" + msg #创建进程池对象 ,进程池中包含4个进程 pool = mp.Pool(processes ... 阅读全文
posted @ 2018-11-09 19:16 IndustriousHe 阅读(226) 评论(0) 推荐(0)
摘要:```python #进程函数的使用 from multiprocessing import Process from time import sleep a = 1 def worker(sec,msg): #当worker作为子进程运行时,对全局量a 的修改只会 #影响在子进程中a的值 ,对父进程没有影响 global a a = 1000 fo... 阅读全文
posted @ 2018-11-09 19:07 IndustriousHe 阅读(650) 评论(0) 推荐(0)
摘要:```python import multiprocessing as mp import os import time #将要做的事封装为函数 def th1(): print(os.getppid(),"----",os.getpid()) print('吃饭早饭') time.sleep(1) print('吃饭午饭') time.sleep(2... 阅读全文
posted @ 2018-11-09 19:03 IndustriousHe 阅读(263) 评论(0) 推荐(0)
摘要:```python os 模块提供大量和系统相关的功能函数接口 os模块的使用是系统相关的 在不同的系统中 可能使用方法不同 import os print('before create process') a = 10 创建新的进程 pid = os.fork() if pid 阅读全文
posted @ 2018-11-09 18:39 IndustriousHe 阅读(377) 评论(0) 推荐(0)
摘要:进程线程网络 多任务编程 : 可以有效的利用计算机资源,同时执行多个任务 进程 : 进程就是程序在计算机中一次执行的过程 进程和程序的区别: 程序是一个静态文件的描述,不占计算机的系统资源 进程是一个动态的过程,占有cpu内存等资源,有一定的生命周期 同一个程序的不同执行过程即为不同的进程 问题1 阅读全文
posted @ 2018-11-09 18:11 IndustriousHe 阅读(747) 评论(0) 推荐(1)