1.信号量

import threading,time

class myThread(threading.Thread):
def run(self): if semaphore.acquire():
print(self.name)
time.sleep(3)
semaphore.release() if __name__=="__main__":
semaphore=threading.Semaphore() thrs=[]
for i in range(100):
thrs.append(myThread())
for t in thrs:
t.start()

2.同步对象

import threading,time
class Boss(threading.Thread): def run(self):
print("BOSS:今晚大家都要加班到22:00。")
print(event.isSet())# False
event.set()
time.sleep(5)
print("BOSS:<22:00>可以下班了。")
print(event.isSet())
event.set() class Worker(threading.Thread):
def run(self): event.wait()# 一旦event被设定,等同于pass print("Worker:哎……命苦啊!")
time.sleep(1)
event.clear()
event.wait()
print("Worker:OhYeah!") if __name__=="__main__":
event=threading.Event() threads=[]
for i in range(5):
threads.append(Worker())
threads.append(Boss())
for t in threads:
t.start()
for t in threads:
t.join() print("ending.....")

3.生产者消费者模型

# import time,random
# import queue,threading
#
# q = queue.Queue()
#
# def Producer(name):
# count = 0
# while count <10:
# print("making........")
# time.sleep(5)
# q.put(count)
# print('Producer %s has produced %s baozi..' %(name, count))
# count +=1
# #q.task_done()
# q.join()
# print("ok......") # def Consumer(name):
# count = 0
# while count <10:
# time.sleep(random.randrange(4))
# # if not q.empty():
# # print("waiting.....")
# #q.join()
# data = q.get()
# print("eating....")
# time.sleep(4)
#
# q.task_done()
# #print(data)
# print('\033[32;1mConsumer %s has eat %s baozi...\033[0m' %(name, data))
# # else:
# # print("-----no baozi anymore----")
# count +=1
#
# p1 = threading.Thread(target=Producer, args=('A君',))
# c1 = threading.Thread(target=Consumer, args=('B君',))
# c2 = threading.Thread(target=Consumer, args=('C君',))
# c3 = threading.Thread(target=Consumer, args=('D君',))
#
# p1.start()
# c1.start()
# c2.start()
# c3.start()

4.递归锁

import  threading
import time class MyThread(threading.Thread): def actionA(self): r_lcok.acquire() #count=1
print(self.name,"gotA",time.ctime())
time.sleep(2)
r_lcok.acquire() #count=2 print(self.name, "gotB", time.ctime())
time.sleep(1) r_lcok.release() #count=1
r_lcok.release() #count=0 def actionB(self): r_lcok.acquire()
print(self.name, "gotB", time.ctime())
time.sleep(2) r_lcok.acquire()
print(self.name, "gotA", time.ctime())
time.sleep(1) r_lcok.release()
r_lcok.release() def run(self): self.actionA()
self.actionB() if __name__ == '__main__': # A=threading.Lock()
# B=threading.Lock() r_lcok=threading.RLock()
L=[] for i in range(5):
t=MyThread()
t.start()
L.append(t) for i in L:
i.join() print("ending....")

5.进程调用

# from multiprocessing import Process
# import time
#
#
# def f(name):
# time.sleep(1)
# print('hello', name,time.ctime())
#
# if __name__ == '__main__':
# p_list=[]
# for i in range(3):
#
# p = Process(target=f, args=('alvin',))
# p_list.append(p)
# p.start()
#
# for i in p_list:
# i.join()
# print('end') from multiprocessing import Process
import time # class MyProcess(Process):
#
# # def __init__(self):
# # super(MyProcess, self).__init__()
# # #self.name = name
#
# def run(self):
# time.sleep(1)
# print ('hello', self.name,time.ctime())
# # if __name__ == '__main__':
# p_list=[]
#
#
# for i in range(3):
# p = MyProcess()
# p.daemon=True
# p.start()
# p_list.append(p)
#
# # for p in p_list:
# # p.join()
#
# print('end') # from multiprocessing import Process
# import os
# import time
#
#
# def info(title):
# print("title:", title)
# print('parent process:', os.getppid())
# print('process id:', os.getpid())
#
# def f(name):
#
# info('function f')
# print('hello', name)
from multiprocessing import Process
import os
import time
def info(title):
print("title",title)
print("parent process:",os.getppid())
print("process id:",os.getpid()) def f(name):
info('function f')
print("hello",name) if __name__ == '__main__':
info('main process line')
time.sleep(1)
print("-------")
p = Process(target=info("yuan"))
p.start()
p.join()
# if __name__ == '__main__':
#
# info('main process line')
#
# time.sleep(1)
# print("------------------")
# p = Process(target=info, args=('yuan',))
# p.start()
# p.join()

day34-python之进程调用的更多相关文章

  1. python之进程与线程

    什么是操作系统       可能很多人都会说,我们平时装的windows7 windows10都是操作系统,没错,他们都是操作系统.还有没有其他的? 想想我们使用的手机,Google公司的Androi ...

  2. Python:进程

    由于GIL的存在,python一个进程同时只能执行一个线程.因此在python开发时,计算密集型的程序常用多进程,IO密集型的使用多线程 1.多进程创建: #创建方法1:将要执行的方法作为参数传给Pr ...

  3. python笔记之调用系统命令

    python笔记之调用系统命令 目前我使用到的python中执行cmd的方式有三种 使用os.system("cmd") 该方法在调用完shell脚本后,返回一个16位的二进制数, ...

  4. python进阶------进程线程(五)

    Python中的IO模型 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别 ...

  5. python进阶------进程线程(三)

    python中的进程 1.multiprocessing模块 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进 ...

  6. python 之进程篇

    多线程给我们的感觉 1.因为GIL的存在,一个进程的多线程同一时刻只能进去一个,感觉是假的并发 2.只适合I/O密集型的任务 3.针对计算密集型,就挂了(变成串行了) 在python中想要充分利用多核 ...

  7. Python之进程

    进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体:在当代 ...

  8. Python之进程 3 - 进程池和multiprocess.Poll

    一.为什么要有进程池? 在程序实际处理问题过程中,忙时会有成千上万的任务需要被执行,闲时可能只有零星任务.那么在成千上万个任务需要被执行的时候,我们就需要去创建成千上万个进程么?首先,创建进程需要消耗 ...

  9. Python之进程 2 - multiprocessing模块

    ​ 我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创建的.因此,运行起来的python程序也是一个进程,那么我们也可以在程序中再创建进程.多个进程可以实现并发效果,也就是说, ...

  10. {Python之进程} 背景知识 什么是进程 进程调度 并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 multiprocess模块 进程池和mutiprocess.Poll

    Python之进程 进程 本节目录 一 背景知识 二 什么是进程 三 进程调度 四 并发与并行 五 同步\异步\阻塞\非阻塞 六 进程的创建与结束 七 multiprocess模块 八 进程池和mut ...

随机推荐

  1. 删除N天前的log日志文件:RollingFileAppender,DailyRollingFileAppender,/etc/cron

    1. 如果您使用的是Log4j,且采用的RollingFileAppender方式, 通过设置maxBackupIndex属性来指定要保留的日志文件数的最大值可以间接实现删除N天前的日志文件. 2. ...

  2. Goroutine调度器

    前言 并发(并行)一致都是编程语言的核心主题,不同于其他语言,例如C/C++语言用户序自行借助pthread创建线程,Golang天然就给出了并发解决方案:goroutine. Goroutine 写 ...

  3. Laravel模型事件的实现原理详解

    模型事件在 Laravel 的世界中,你对 Eloquent 大多数操作都会或多或少的触发一些模型事件,下面这篇文章主要给大家介绍了关于Laravel模型事件的实现原理,文中通过示例代码介绍的非常详细 ...

  4. 微信小程序的跳转navigateTo()和redirectTo()用法和区别

    原文链接:https://blog.csdn.net/u013128651/article/details/79736410  wx.navigateTo({}) ,保留当前页面,跳转到应用内的某个页 ...

  5. EasyNVS摄像机公网全终端无插件网页摄像机直播管理服务运行出现“请求服务不存在或已停止”

    背景分析 了解我们产品线的小伙伴都知道,EasyNVR产品主要用于互联网安防直播,通过EasyNVR拉取摄像机的RTSP视频流,客户端通过访问EasyNVR服务端就可以实现音视频流分发.在此情况下会出 ...

  6. junit单元测试不通过报documentationPluginsBootstrapper相关异常

    这是因为Spring整合springfox-swagger2后导致的,错误信息如下: -- ::, [main] [WARN] [org.springframework.context.support ...

  7. sort_buffer_size, Sort_merge_passes关系

    对于事务性工作负载是通常最快这个大小设置为32K,并且也是允许的最小尺寸.您应该谨慎使用它设置为较大的值,因为这可以很容易地降低性能. 如果所有的数据进行排序不适合在指定缓冲区大小的MySQL第一种类 ...

  8. Spring MVC原理图及其重要组件

    一.Spring MVC原理图: ps: springmvc的运行流程为图中数字序号 二.springmvc的重要组件: 1)前端控制器:DispatchServlet(不需要程序员开发) 接收请求, ...

  9. [CMD] Jenkins上执行robot命令如果出现fail不往下走其他的CMD命令了

    需要在后面加上||exit 0 robot -o %disSection%.xml --include %disSection% -v ENV:%envBmk% .||exit 0

  10. Python-09-文件处理

    一.文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 #1. 打开文件,得到文件句柄并赋值给一个变量 f=open('a.txt','r',encoding='u ...