君子好逑 发表于 2021-1-23 14:38:24

多线程

import time
import threading

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

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

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

t1.start()
t2.start()

t1.join()
t2.join()

print('任务完成啦 时间:%s'%time.ctime())
都是这一段代码,运行的时候有的时候结果是对的,有的时候结果是错的。有大佬能给我指点一下迷津吗{:10_254:}

°蓝鲤歌蓝 发表于 2021-1-23 14:54:57

打印的问题吧

kogawananari 发表于 2021-1-23 15:11:45

print flush=True试试

君子好逑 发表于 2021-1-23 15:29:07

kogawananari 发表于 2021-1-23 15:11
print flush=True试试

还是不行{:10_266:}

kogawananari 发表于 2021-1-23 15:54:08

君子好逑 发表于 2021-1-23 15:29
还是不行

那我没辙了用日志模块输出最好那个是线程安全的
{:10_292:}

君子好逑 发表于 2021-1-23 16:08:24

kogawananari 发表于 2021-1-23 15:54
那我没辙了用日志模块输出最好那个是线程安全的

怎么用日志模块输出大佬

君子好逑 发表于 2021-1-23 16:09:01

kogawananari 发表于 2021-1-23 15:54
那我没辙了用日志模块输出最好那个是线程安全的

刚开始学python没多久,好多东西不会用{:10_266:}

hrp 发表于 2021-1-23 17:19:27

试试使用线程锁锁定打印函数,使同一时刻只能有一个线程print
import time
import threading

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

def mprint(*args, **kwargs):
    # 获得锁
    alock.acquire()
    print(*args, **kwargs)
    # 释放锁
    alock.release()

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

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

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

t1.start()
t2.start()

t1.join()
t2.join()

print('任务完成啦 时间:%s'%time.ctime())

君子好逑 发表于 2021-1-23 17:22:06

hrp 发表于 2021-1-23 17:19
试试使用线程锁锁定打印函数,使同一时刻只能有一个线程print

大佬牛逼

kogawananari 发表于 2021-1-23 17:44:40

君子好逑 发表于 2021-1-23 16:09
刚开始学python没多久,好多东西不会用

import logging
##级别依次提高
logging.debug('排错信息')
logging.info('正常信息')
logging.warning('警告信息')
logging.error('错误信息')
logging.critical('严重错误信息')

kogawananari 发表于 2021-1-23 17:51:09

import time
import threading
import logging
logging.basicConfig(level=logging.DEBUG)

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

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

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

t1.start()
t2.start()

t1.join()
t2.join()

print('任务完成啦 时间:%s'%time.ctime())

君子好逑 发表于 2021-1-23 18:09:36

kogawananari 发表于 2021-1-23 17:51


都是大佬,i了i了
页: [1]
查看完整版本: 多线程