1.信号量

  1. import threading,time
  2.  
  3. class myThread(threading.Thread):
  4. def run(self):
  5.  
  6. if semaphore.acquire():
  7. print(self.name)
  8. time.sleep(3)
  9. semaphore.release()
  10.  
  11. if __name__=="__main__":
  12. semaphore=threading.Semaphore()
  13.  
  14. thrs=[]
  15. for i in range(100):
  16. thrs.append(myThread())
  17. for t in thrs:
  18. t.start()

2.同步对象

  1. import threading,time
  2. class Boss(threading.Thread):
  3.  
  4. def run(self):
  5. print("BOSS:今晚大家都要加班到22:00。")
  6. print(event.isSet())# False
  7. event.set()
  8. time.sleep(5)
  9. print("BOSS:<22:00>可以下班了。")
  10. print(event.isSet())
  11. event.set()
  12.  
  13. class Worker(threading.Thread):
  14. def run(self):
  15.  
  16. event.wait()# 一旦event被设定,等同于pass
  17.  
  18. print("Worker:哎……命苦啊!")
  19. time.sleep(1)
  20. event.clear()
  21. event.wait()
  22. print("Worker:OhYeah!")
  23.  
  24. if __name__=="__main__":
  25. event=threading.Event()
  26.  
  27. threads=[]
  28. for i in range(5):
  29. threads.append(Worker())
  30. threads.append(Boss())
  31. for t in threads:
  32. t.start()
  33. for t in threads:
  34. t.join()
  35.  
  36. print("ending.....")

3.生产者消费者模型

  1. # import time,random
  2. # import queue,threading
  3. #
  4. # q = queue.Queue()
  5. #
  6. # def Producer(name):
  7. # count = 0
  8. # while count <10:
  9. # print("making........")
  10. # time.sleep(5)
  11. # q.put(count)
  12. # print('Producer %s has produced %s baozi..' %(name, count))
  13. # count +=1
  14. # #q.task_done()
  15. # q.join()
  16. # print("ok......")
  17.  
  18. # def Consumer(name):
  19. # count = 0
  20. # while count <10:
  21. # time.sleep(random.randrange(4))
  22. # # if not q.empty():
  23. # # print("waiting.....")
  24. # #q.join()
  25. # data = q.get()
  26. # print("eating....")
  27. # time.sleep(4)
  28. #
  29. # q.task_done()
  30. # #print(data)
  31. # print('\033[32;1mConsumer %s has eat %s baozi...\033[0m' %(name, data))
  32. # # else:
  33. # # print("-----no baozi anymore----")
  34. # count +=1
  35. #
  36. # p1 = threading.Thread(target=Producer, args=('A君',))
  37. # c1 = threading.Thread(target=Consumer, args=('B君',))
  38. # c2 = threading.Thread(target=Consumer, args=('C君',))
  39. # c3 = threading.Thread(target=Consumer, args=('D君',))
  40. #
  41. # p1.start()
  42. # c1.start()
  43. # c2.start()
  44. # c3.start()

4.递归锁

  1. import threading
  2. import time
  3.  
  4. class MyThread(threading.Thread):
  5.  
  6. def actionA(self):
  7.  
  8. r_lcok.acquire() #count=1
  9. print(self.name,"gotA",time.ctime())
  10. time.sleep(2)
  11. r_lcok.acquire() #count=2
  12.  
  13. print(self.name, "gotB", time.ctime())
  14. time.sleep(1)
  15.  
  16. r_lcok.release() #count=1
  17. r_lcok.release() #count=0
  18.  
  19. def actionB(self):
  20.  
  21. r_lcok.acquire()
  22. print(self.name, "gotB", time.ctime())
  23. time.sleep(2)
  24.  
  25. r_lcok.acquire()
  26. print(self.name, "gotA", time.ctime())
  27. time.sleep(1)
  28.  
  29. r_lcok.release()
  30. r_lcok.release()
  31.  
  32. def run(self):
  33.  
  34. self.actionA()
  35. self.actionB()
  36.  
  37. if __name__ == '__main__':
  38.  
  39. # A=threading.Lock()
  40. # B=threading.Lock()
  41.  
  42. r_lcok=threading.RLock()
  43. L=[]
  44.  
  45. for i in range(5):
  46. t=MyThread()
  47. t.start()
  48. L.append(t)
  49.  
  50. for i in L:
  51. i.join()
  52.  
  53. print("ending....")

5.进程调用

  1. # from multiprocessing import Process
  2. # import time
  3. #
  4. #
  5. # def f(name):
  6. # time.sleep(1)
  7. # print('hello', name,time.ctime())
  8. #
  9. # if __name__ == '__main__':
  10. # p_list=[]
  11. # for i in range(3):
  12. #
  13. # p = Process(target=f, args=('alvin',))
  14. # p_list.append(p)
  15. # p.start()
  16. #
  17. # for i in p_list:
  18. # i.join()
  19. # print('end')
  20.  
  21. from multiprocessing import Process
  22. import time
  23.  
  24. # class MyProcess(Process):
  25. #
  26. # # def __init__(self):
  27. # # super(MyProcess, self).__init__()
  28. # # #self.name = name
  29. #
  30. # def run(self):
  31. # time.sleep(1)
  32. # print ('hello', self.name,time.ctime())
  33. #
  34.  
  35. # if __name__ == '__main__':
  36. # p_list=[]
  37. #
  38. #
  39. # for i in range(3):
  40. # p = MyProcess()
  41. # p.daemon=True
  42. # p.start()
  43. # p_list.append(p)
  44. #
  45. # # for p in p_list:
  46. # # p.join()
  47. #
  48. # print('end')
  49.  
  50. # from multiprocessing import Process
  51. # import os
  52. # import time
  53. #
  54. #
  55. # def info(title):
  56. # print("title:", title)
  57. # print('parent process:', os.getppid())
  58. # print('process id:', os.getpid())
  59. #
  60. # def f(name):
  61. #
  62. # info('function f')
  63. # print('hello', name)
  64. from multiprocessing import Process
  65. import os
  66. import time
  67. def info(title):
  68. print("title",title)
  69. print("parent process:",os.getppid())
  70. print("process id:",os.getpid())
  71.  
  72. def f(name):
  73. info('function f')
  74. print("hello",name)
  75.  
  76. if __name__ == '__main__':
  77. info('main process line')
  78. time.sleep(1)
  79. print("-------")
  80. p = Process(target=info("yuan"))
  81. p.start()
  82. p.join()
  83. # if __name__ == '__main__':
  84. #
  85. # info('main process line')
  86. #
  87. # time.sleep(1)
  88. # print("------------------")
  89. # p = Process(target=info, args=('yuan',))
  90. # p.start()
  91. # 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. Mysql按日、周、月进行分组统计

    我们在用 Mysql 制作数据可视化图表时候,经常需要按照天.周.月等不同的粒度对数据进行分组统计.而我们的时间可能是 “2017/12/5 0:0:0” 这种准确的时间. 所以在进行分组之前我们需要 ...

  2. java使用ssh连接Linux并执行命令

     方式1:通过设置账号密码和链接地址 maven pom.xml配置: <dependency>         <groupId>com.jcraft</groupId ...

  3. 系统运维工程师装逼完全指南(转载Mark)

    1.全球化的认证有助于提升逼格,什么OCM.CCIE.RHCA.CISSP等等能考都考,再不济,也要有一张系统架构设计师或者网络规划设计师的信产部认证.每过一个认证,逼格提升一档. 2.TCP/IP协 ...

  4. Java项目main方法启动的两种方式

    1.打包时指定了主类,可以直接用java -jar xxx.jar. <!--main方法打包jar包插件--> <plugin> <artifactId>mave ...

  5. Spring Boot拦截器实现并和swagger集成后使用拦截器的配置问题

    1. 定义拦截器 LoginInterceptor LoginInterceptor.java是整个登录认证模块中的核心类之一,它实现了HandlerInterceptor类,由它来拦截并过滤到来的每 ...

  6. mysql查询列为空

    SELECT * FROM `表名` WHERE ISNULL(列名)

  7. 通过元类创建一个Python类

    通过元类创建一个Python类 最开始学pytohn的时候我们这样定义类 class ClassName: pass 当熟悉了元类的概念之后我们还可以这样创建 ClassName = type(&qu ...

  8. 05 javascript知识点---BOM和DOM

    1.DOM简单学习(为了满足案例要求) 功能:控制html文档的内容获取页面标签(元素)对象:Element document.getElementById("id值"):通过元素 ...

  9. 使用JMeter进行Apache Kafka负载测试

    1.卡夫卡负载测试 在这个Apache Kafka教程中,我们将了解如何使用Apache JMeter,如何在Apache Kafka上执行Kafka负载测试.此外,这个Kafka负载测试教程教我们如 ...

  10. 使用guava cache在本地缓存热点数据

    某些热点数据在短时间内可能会被成千上万次访问,所以除了放在redis之外,还可以放在本地内存,也就是JVM的内存中. 我们可以使用google的guava cache组件实现本地缓存,之所以选择gua ...