鱼C论坛

 找回密码
 立即注册
查看: 1910|回复: 11

[已解决]多线程

[复制链接]
发表于 2021-1-23 14:38:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import time
  2. import threading

  3. def music(name,times1):
  4.     for i in range (times1):
  5.         print('在听音乐 %s 时间 %s name:%s id:%s'%(name ,time.ctime() ,t1.getName() ,t1.ident))
  6.         time.sleep(1)

  7. def movie(name,times2):
  8.     for j in range (times2):
  9.         print('在看电影 %s 时间 %s name:%s id:%s'%(name ,time.ctime() ,threading.Thread.getName(t2) ,t2.ident))
  10.         time.sleep(1)

  11. t1 = threading.Thread(target=music,args=('虞兮叹',3))
  12. t1.setName('music thread')
  13. t2 = threading.Thread(target=movie,args=('魑魅魍魉',5),name='movie thread')

  14. t1.start()
  15. t2.start()

  16. t1.join()
  17. t2.join()

  18. print('任务完成啦 时间:%s'%time.ctime())
复制代码

都是这一段代码,运行的时候有的时候结果是对的,有的时候结果是错的。有大佬能给我指点一下迷津吗
最佳答案
2021-1-23 17:19:27
试试使用线程锁锁定打印函数,使同一时刻只能有一个线程print
  1. import time
  2. import threading

  3. # 实例化一个线程锁
  4. alock = threading.Lock()

  5. def mprint(*args, **kwargs):
  6.     # 获得锁
  7.     alock.acquire()
  8.     print(*args, **kwargs)
  9.     # 释放锁
  10.     alock.release()

  11. def music(name,times1):
  12.     for i in range (times1):
  13.         mprint('在听音乐 %s 时间 %s name:%s id:%s'%(name ,time.ctime() ,t1.getName() ,t1.ident))
  14.         time.sleep(1)

  15. def movie(name,times2):
  16.     for j in range (times2):
  17.         mprint('在看电影 %s 时间 %s name:%s id:%s'%(name ,time.ctime() ,threading.Thread.getName(t2) ,t2.ident))
  18.         time.sleep(1)

  19. t1 = threading.Thread(target=music,args=('虞兮叹',3))
  20. t1.setName('music thread')
  21. t2 = threading.Thread(target=movie,args=('魑魅魍魉',5),name='movie thread')

  22. t1.start()
  23. t2.start()

  24. t1.join()
  25. t2.join()

  26. print('任务完成啦 时间:%s'%time.ctime())
复制代码
错误1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-23 14:54:57 | 显示全部楼层
打印的问题吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-23 15:11:45 | 显示全部楼层
print flush=True试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-23 15:29:07 | 显示全部楼层

还是不行
错误1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-23 15:54:08 | 显示全部楼层


那我没辙了  用日志模块输出最好  那个是线程安全的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-23 16:08:24 | 显示全部楼层
kogawananari 发表于 2021-1-23 15:54
那我没辙了  用日志模块输出最好  那个是线程安全的

怎么用日志模块输出大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-23 16:09:01 | 显示全部楼层
kogawananari 发表于 2021-1-23 15:54
那我没辙了  用日志模块输出最好  那个是线程安全的

刚开始学python没多久,好多东西不会用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-23 17:19:27 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
试试使用线程锁锁定打印函数,使同一时刻只能有一个线程print
  1. import time
  2. import threading

  3. # 实例化一个线程锁
  4. alock = threading.Lock()

  5. def mprint(*args, **kwargs):
  6.     # 获得锁
  7.     alock.acquire()
  8.     print(*args, **kwargs)
  9.     # 释放锁
  10.     alock.release()

  11. def music(name,times1):
  12.     for i in range (times1):
  13.         mprint('在听音乐 %s 时间 %s name:%s id:%s'%(name ,time.ctime() ,t1.getName() ,t1.ident))
  14.         time.sleep(1)

  15. def movie(name,times2):
  16.     for j in range (times2):
  17.         mprint('在看电影 %s 时间 %s name:%s id:%s'%(name ,time.ctime() ,threading.Thread.getName(t2) ,t2.ident))
  18.         time.sleep(1)

  19. t1 = threading.Thread(target=music,args=('虞兮叹',3))
  20. t1.setName('music thread')
  21. t2 = threading.Thread(target=movie,args=('魑魅魍魉',5),name='movie thread')

  22. t1.start()
  23. t2.start()

  24. t1.join()
  25. t2.join()

  26. print('任务完成啦 时间:%s'%time.ctime())
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-23 17:22:06 | 显示全部楼层
hrp 发表于 2021-1-23 17:19
试试使用线程锁锁定打印函数,使同一时刻只能有一个线程print

大佬牛逼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-23 17:44:40 | 显示全部楼层
君子好逑 发表于 2021-1-23 16:09
刚开始学python没多久,好多东西不会用

import logging
##级别依次提高
logging.debug('排错信息')
logging.info('正常信息')
logging.warning('警告信息')
logging.error('错误信息')
logging.critical('严重错误信息')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-23 17:51:09 | 显示全部楼层
  1. import time
  2. import threading
  3. import logging
  4. logging.basicConfig(level=logging.DEBUG)

  5. def music(name,times1):
  6.     for i in range (times1):
  7.         logging.info('在听音乐 %s 时间 %s name:%s id:%s'%(name ,time.ctime() ,t1.getName() ,t1.ident))
  8.         time.sleep(1)

  9. def movie(name,times2):
  10.     for j in range (times2):
  11.         logging.info('在看电影 %s 时间 %s name:%s id:%s'%(name ,time.ctime() ,threading.Thread.getName(t2) ,t2.ident))
  12.         time.sleep(1)

  13. t1 = threading.Thread(target=music,args=('虞兮叹',3))
  14. t1.setName('music thread')
  15. t2 = threading.Thread(target=movie,args=('魑魅魍魉',5),name='movie thread')

  16. t1.start()
  17. t2.start()

  18. t1.join()
  19. t2.join()

  20. print('任务完成啦 时间:%s'%time.ctime())
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-23 18:09:36 | 显示全部楼层

都是大佬,i了i了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 22:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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