python多线程输出_Python-线程正在同时打印,弄乱了文本输出

I am using 4 threads in an application which return text I would like to print to the user.

Since I would like to avoid the threads to independently print those texts, I have created a class to administrate it...

I don't know what I am doing wrong here, but it is still not working.

The code you can see below:

from threading import Thread

import time

import random

class Creature:

def __init__(self, name, melee, shielding, health, mana):

self.name = name

self.melee = melee

self.shielding = shielding

self.health = health

self.mana = mana

def attack(self, attacker, opponent, echo):

while 0 != 1:

time.sleep(1)

power = random.randint(1, attacker.melee)

resistance = random.randint(1, opponent.shielding)

resultant = power - resistance

if resistance > 0:

opponent.health -= resistance

if opponent.health < 0:

msg = opponent.name, " is dead"

echo.message(msg)

quit()

else:

msg = opponent.name, " lost ", resistance, " hit points due to an attack by ", attacker.name

echo.message(msg)

def healing(self, healed, echo):

while 0 != 1:

time.sleep(1)

if self.mana >= 25:

if healed.health >= 0:

if healed.health < 50:

life = random.randint(1, 50)

self.mana -= 25

healed.health += life

if healed.health > 100:

healed.health = 100

msg = healed.name, " has generated himself and now has ", self.health, " hit points"

echo.message(msg)

else:

quit()

class echo:

def message(self, msg):

print msg

myEcho = echo()

Monster = Creature("Wasp", 30, 15, 100, 100)

Player = Creature("Knight", 25, 20, 100, 100)

t1 = Thread(target = Player.attack, args = (Monster, Player, myEcho))

t1.start()

t2 = Thread(target = Monster.attack, args = (Player, Monster, myEcho))

t2.start()

t3 = Thread(target=Player.healing(Player, myEcho), args=())

t3.start()

t4 = Thread(target=Monster.healing(Monster, myEcho), args=())

t4.start()

Here you can see the messed up outputs:

*('Wasp'('Knight', ' l, ' lost ', ost 13, ' hit points ', 4, due to an attack by '' hi, 'Waspt poi')nts d

ue to an attack by ', 'Knight')

('Wasp', ' lost ', 12, ' hit points due to an attack by ', 'Knight')

('Knight', ' lost ', 17, ' hit points due to an attack by ', 'Wasp')

('Wasp', ' lost ', 6, ' hit points due to an attack by ', 'Knight'('Knight')

, ' lost ', 1, ' hit points due to an attack by ', 'Wasp')

('Wasp', ' lost ', 5, ' hit points due to an attack by ', 'Knight')

('Knight', ' lost ', 13, ' hit points due to an attack by ', 'Wasp')

(('Wa'Knighsp't', , ' los' lostt ' ', , 32, ' hit points due to an attack by ', 'Knight')

, ' hit points due to an attack by ', 'Wasp')*

Do you guys have any idea how to fix this issue?

Thanks!

解决方案

Use a threading.Semaphore to ensure that there won't be any conflicts:

screenlock = Semaphore(value=1) # You'll need to add this to the import statement.

Then, before you call echo.message, insert this line in order to acquire the right to output:

screenlock.acquire()

and then this line after you call echo.message so as to allow another thread to print:

screenlock.release()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值