简体   繁体   English

有没有办法可以暂停 function 但不影响 Python 中的其他功能?

[英]Is there a way that i can pause a function but not effect other functions in Python?

So basically i am coding a discord bot in python.所以基本上我在 python 中编码一个 discord 机器人。 It is a bot where you can buy/sell things from a shop, and go out farming or mining for resources.它是一个机器人,您可以在其中从商店购买/出售东西,并且 go 可以进行农业或采矿以获取资源。 The main bit i need help with is making the function rest for around 5 minutes.我需要帮助的主要一点是制作 function rest 大约 5 分钟。 This bot will probably be used multiple times in 5 minutes, so i can't pause the whole code.这个机器人可能会在 5 分钟内被多次使用,所以我不能暂停整个代码。 obviously time.sleep will not work since it pauses the whole script.显然 time.sleep 将不起作用,因为它会暂停整个脚本。 I have tried a few different things but they didn't work well and i couldn't find much else on the internet.我尝试了一些不同的方法,但效果不佳,我在互联网上找不到太多其他东西。 My code is:我的代码是:

    @client.command()
async def farm(ctx, item):
    fakelocation = 11
    await get_bank_data()
    users = await get_bank_data()
    if users[str(ctx.author.id)]["location"] == "Farm":
        item = item.lower()
        returnholder = 6
        product = "none"
        user = ctx.author
        users = await get_bank_data()
        if item == "potato":
            product = "potato"
            amount = random.randrange(2, 10)
            await ctx.send("Your crops will be given to you at the end of the session!")
            returnholder = 5
        if item == "carrot":
            product = "carrot"
            amount = random.randrange(4, 12)
            await ctx.send("Your crops will be given to you at the end of the session!")
            returnholder = 5
        if item == "wheat":
            product = "wheat"
            amount = random.randrange(7, 15)
            await ctx.send("Your crops will be given to you at the end of the session!")
            returnholder = 5
        if item == "apple":
            product = "apple"
            amount = random.randrange(2, 13)
            await ctx.send("Your crops will be given to you at the end of the session!")
            returnholder = 5
        if item == "banana":
            product = "banana"
            amount = random.randrange(4, 10)
            await ctx.send("Your crops will be given to you at the end of the session!")
            returnholder = 5
        if item == "orange":
            product = "orange"
            amount = random.randrange(3, 8)
            await ctx.send("Your crops will be given to you at the end of the session!")
            returnholder = 5
        if returnholder == 6:
            await ctx.send("That crop does not exist here!")
            return
        try:
            index = 0
            t = None
            for thing in users[str(user.id)]["bag"]:
                n = thing["item"]
                if n == item:
                    old_amt = thing["amount"]
                    new_amt = old_amt + amount
                    users[str(user.id)]["bag"][index]["amount"] = new_amt
                    t = 1
                    break
                index += 1
            if t == None:
                obj = {"item": product, "amount": amount}
                users[str(user.id)]["bag"].append(obj)
        except:
            obj = {"item": product, "amount": amount}
            users[str(user.id)]["bag"] = [obj]
        with open("mainbank.json", "w") as f:
            json.dump(users, f)
        fakelocation = 4
    if fakelocation == 11:
        await ctx.send("Please move to the Farm to farm!")
        return

This is the code and it is relatively similar to the Mine function.这是代码,它与我的 function 比较相似。 it checks what the item is and then if it exists it will give you a random amount of items.I want to make it stop first thing in try: since otherwise it will give you the items before the time is up.它会检查项目是什么,然后如果它存在,它会给你随机数量的项目。我想让它在try:因为否则它会在时间结束之前给你项目。 Thanks for any help!!谢谢你的帮助!!

import asyncio

await asyncio.sleep(SECONDS)

This was the answer for me.这就是我的答案。 Thanks @TinNguyen for answering my question simply.感谢@TinNguyen 简单地回答我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 如何在其他线程的线程上暂停? - Python - How can I pause at thread by other thread? 如何暂停Python中的所有其他线程? - How can I pause all the other threads in Python? 如何在两个功能之间暂停? - How can I pause between two functions? 我可以在多行的python中编写lambda函数,然后调用其他函数吗? - Can I write a lambda function in python of more than one line, and call other functions? 在python中定义一个主要函数,该函数可以在python中运行其他函数 - Define a main function in python which can run other functions in python 如何在其他函数可以在python中访问的函数中创建变量? - How do I create a variable in a function that other functions can access in python? 如何更改每个函数中的全局变量而不影响 Python 中的函数? - how can I change the global variable in each function without the functions affecting each other in Python? 我可以在Python中创建一个主函数来接受一行其他函数吗? - Can I create a master function in Python to take a single line of other functions? Python 写一个循环的方式 function 可以接受自定义函数 - Python way to write a looping function that can accept customizable functions 如何在 Python 程序中调用/执行其他函数 - How can I call/execute other functions within a Python programm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM