鱼C论坛

 找回密码
 立即注册
查看: 2336|回复: 2

1,2,3,4共4个问题,2,4是同一类型的,全部放进注释中了

[复制链接]
发表于 2022-10-23 15:44:06 | 显示全部楼层 |阅读模式
1鱼币
本帖最后由 Boibot 于 2022-10-23 15:54 编辑
  1. import threading  # 线程模块
  2. import time


  3. def eat_breakfast():
  4.     time.sleep(3)
  5.     print("You eat breakfast")


  6. def drink_coffee():
  7.     time.sleep(4)
  8.     print("You drank coffee")


  9. def study():
  10.     time.sleep(5)
  11.     print("You finish studying")


  12. x = threading.Thread(target=eat_breakfast, args=())  # 1.target=function,这里的函数加不加括号是一样的吗?
  13. x.start()

  14. y = threading.Thread(target=drink_coffee, args=())
  15. y.start()

  16. z = threading.Thread(target=study, args=())
  17. z.start()

  18. x.join()     # thread synchronization(线程同步)
  19. y.join()     # thread synchronize and join(线程同步并加入)
  20. z.join()


  21. # eat_breakfast()
  22. # drink_coffee()
  23. # study()


  24. print(threading.active_count())  # 函数名活动计数
  25. print(threading.enumerate())  # 函数名枚举
  26. print(time.perf_counter())  # performance counter function(函数名性能计数器)

  27. console window output:  # 2.time.perf_counter()是以秒为单位,我的输出相较正确答案大了上万倍
  28. You eat breakfast
  29. You drank coffee
  30. You finish studying
  31. 1
  32. [<_MainThread(MainThread, started 14744)>]
  33. 9517.6617491

  34. from multiprocessing import Process, cpu_count
  35. import time


  36. def counter(num):
  37.     count = 0
  38.     while count < num:
  39.         count += 1


  40. def main():

  41.     print(cpu_count())

  42.     a = Process(target=counter, args=(12500000,))  # 3.Process(target=counter, args=(12500000,)),Pruocess函数的args参数是元组,里面的num元素为什么必须要加","逗号
  43.     b = Process(target=counter, args=(12500000,))
  44.     c = Process(target=counter, args=(12500000,))
  45.     d = Process(target=counter, args=(12500000,))
  46.     e = Process(target=counter, args=(12500000,))
  47.     f = Process(target=counter, args=(12500000,))
  48.     g = Process(target=counter, args=(12500000,))
  49.     h = Process(target=counter, args=(12500000,))

  50.     a.start()
  51.     b.start()
  52.     c.start()
  53.     d.start()
  54.     e.start()
  55.     f.start()
  56.     g.start()
  57.     h.start()

  58.     a.join()
  59.     b.join()
  60.     c.join()
  61.     d.join()
  62.     e.join()
  63.     f.join()
  64.     g.join()
  65.     h.join()

  66.     print("Finished in", time.perf_counter(), "seconds")


  67. if __name__ == '__main__':
  68.     main()

  69. console window output:  # 4.time.perf_counter()是以秒为单位,我的输出相较正确答案大了上万倍
  70. 16
  71. Finished in 75426.6729955 seconds
复制代码



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-23 15:50:14 | 显示全部楼层
都学到线程了, time.perf_counter() 都不会用,即使不会用,也应该会查一下文档或相关例子吧。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-10-23 15:58:55 | 显示全部楼层
阿奇_o 发表于 2022-10-23 15:50
都学到线程了, time.perf_counter() 都不会用,即使不会用,也应该会查一下文档或相关例子吧。。{:10_277: ...

刚学到的,对着视频打完,发现输出跟答案对不上,找了time.perf_counter() 官方文档和一些网站的解释,并不能解释为啥我的输出是上万秒
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-28 23:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表