如何在Python中创建并行任务

巴西费利佩

在初学者的项目中,我正在为电报组创建一个战斗游戏,一个玩家向另一个挑战,然后在聊天中发生战斗。现在,我想为其实现“超时”功能。我设法使它使用_thread起作用:

import _thread
class Battle:
    def __init__(self, chat_id):
        self.chat = chat
        print('Instance of battle started on chat %s' % self.chat)
        self.pcount = 0
        self.p1score = 0
        self.p2score = 0

    def reset(self):
        self.pcount = 0
        self.p1score = 0
        self.p2score = 0

def timeout(chat_id):
    time.sleep(90)
    battle = battledic[chat_id]
    if battledic[chat_id].timer == 1:
        sendmessage(chat_id, 'no one accepted') # first parameter is the chat address and the second one is the message
        battle.reset()
    else:
        return

battle = Battle(chat_id)
if message == "challenge":
    if battle.pcount == 0:
        _thread.start_new_thread(timeout, (chat_id,))
...

因此,基本上我正在做的是调用超时功能,该功能将在另一个线程中睡眠90秒,然后检查是否只有一个玩家(pcount == 1)进行战斗。 ,它会向聊天室发送一条消息,告知他们战斗未开始,然后重置战斗实例。

这里的问题是我经常提出异常,即使它在大多数情况下都能正常工作,但有时也达不到预期的效果,导致正在进行的战斗结束。经过研究,我发现这样做并不是理想的方式,但是我找不到更好的解决方案,也许我也想同时进行其他聊天中的其他战斗。在这种情况下应该使用什么?合作多任务处理?多处理?trheading模块?感谢您完成此任务的任何提示!

瓦斯

我已经让你开始了。我不确定您将如何聊天或记录谁在玩。

我最初的想法是运行线程,但是while循环更容易。

# from threading import Thread
from time import sleep

no_one = True
battledic = {} # your dict here. It may be better to use a list of objects (classes) for your opponents

class Battle(object):
    def __init__(self, chat_id):
        self.chat_id = chat_id
        self.pcount = 0
        self.p1score = 0
        self.p2score = 0
        print('Instance of battle started on chat %s' % self.chat_id)

    def reset(self):
        self.pcount = 0
        self.p1score = 0
        self.p2score = 0

def wheres_waldo():
    x = 0; no_one = True
    while x < 90: # waits 90 seconds unless found
        for player in battledic.keys():
            if player.is_requesting: # found someone
                no_one = False
                break # quit looking
        else:
            print('No one yet')
        sleep(1); x+= 1
    if no_one:
        sendmessage(chat_id, 'no one accepted') # first parameter is the chat address and the second one is the message
        battle.reset()
    else:
        sendmessage(chat_id, 'Found someone')

def sendmessage(chat_id,message):
    print('%s on chat %s' % (message,chat_id))

battle = Battle(chat_id)
while True:
    message = raw_input('Command? ')
    if message == "challenge":
        if battle.pcount == 0:
            wheres_waldo()
            # first thoughts were to run threads but the while loop is easier
            # wait = Thread(target=timeout, args=(chat_id))
            # where = Thread(target=wheres_waldo)
            # wait.start();where.start()
            # wait.join();where.join()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在结构中并行执行多个任务

来自分类Dev

如何在C#中增加并行任务的数量

来自分类Dev

如何在并行C#中触发任务列表

来自分类Dev

创建并行任务,如何限制新任务的创建?

来自分类Dev

如何在python中并行解析?

来自分类Dev

如何在 Python 中并行处理列表?

来自分类Dev

如何在共享任务列表中创建任务?

来自分类Dev

如何在 asyncio 和 transport_base 类中创建 python 并行套接字?

来自分类Dev

如何在 Python 中创建和使用 http 并行管理器?

来自分类Dev

如何在Python中使用aiohttp或asyncio创建并行循环?

来自分类Dev

创建CodePipeline之前如何在CloudFormation中创建ECS任务

来自分类Dev

我将如何在 python 中创建一个(异步/线程/任务)背景队列?

来自分类Dev

在python中创建并行for循环

来自分类Dev

如何在通用应用程序中禁用任务并行库的ETW EventSource?

来自分类Dev

如何在C#中限制并行任务的最大数量

来自分类Dev

如何在 Airflow DAG 中并行化类似的 BashOperator 任务但不同的参数

来自分类Dev

如何在Python中并行循环以更改字典中的对象?

来自分类Dev

如何在 Unity 中并行处理潜在的异步聚合任务并在每个任务完成时处理它们

来自分类Dev

如何在Gradle插件中动态创建任务?

来自分类Dev

如何在Verilog中创建和使用任务

来自分类Dev

如何在Shell脚本中创建循环以执行特定任务?

来自分类Dev

如何在Debian中创建用户定义的任务?

来自分类Dev

如何在Gradle插件中动态创建任务?

来自分类Dev

如何在Luigi中创建非持久性任务?

来自分类Dev

如何在类内的python中并行化for?

来自分类Dev

如何在Python中并行化I / O绑定操作?

来自分类Dev

如何在Python中并行扫描许多超参数集?

来自分类Dev

如何在python的子类中并行化进程

来自分类Dev

如何在并行中编写2个Python装饰器

Related 相关文章

  1. 1

    如何在结构中并行执行多个任务

  2. 2

    如何在C#中增加并行任务的数量

  3. 3

    如何在并行C#中触发任务列表

  4. 4

    创建并行任务,如何限制新任务的创建?

  5. 5

    如何在python中并行解析?

  6. 6

    如何在 Python 中并行处理列表?

  7. 7

    如何在共享任务列表中创建任务?

  8. 8

    如何在 asyncio 和 transport_base 类中创建 python 并行套接字?

  9. 9

    如何在 Python 中创建和使用 http 并行管理器?

  10. 10

    如何在Python中使用aiohttp或asyncio创建并行循环?

  11. 11

    创建CodePipeline之前如何在CloudFormation中创建ECS任务

  12. 12

    我将如何在 python 中创建一个(异步/线程/任务)背景队列?

  13. 13

    在python中创建并行for循环

  14. 14

    如何在通用应用程序中禁用任务并行库的ETW EventSource?

  15. 15

    如何在C#中限制并行任务的最大数量

  16. 16

    如何在 Airflow DAG 中并行化类似的 BashOperator 任务但不同的参数

  17. 17

    如何在Python中并行循环以更改字典中的对象?

  18. 18

    如何在 Unity 中并行处理潜在的异步聚合任务并在每个任务完成时处理它们

  19. 19

    如何在Gradle插件中动态创建任务?

  20. 20

    如何在Verilog中创建和使用任务

  21. 21

    如何在Shell脚本中创建循环以执行特定任务?

  22. 22

    如何在Debian中创建用户定义的任务?

  23. 23

    如何在Gradle插件中动态创建任务?

  24. 24

    如何在Luigi中创建非持久性任务?

  25. 25

    如何在类内的python中并行化for?

  26. 26

    如何在Python中并行化I / O绑定操作?

  27. 27

    如何在Python中并行扫描许多超参数集?

  28. 28

    如何在python的子类中并行化进程

  29. 29

    如何在并行中编写2个Python装饰器

热门标签

归档