简体   繁体   English

Python:如何在未使用 X 分钟后删除变量?

[英]Python: How can I delete a variable after being unused for X minutes?

I have the following code:我有以下代码:

from discord.ext import commands

bot = commands.Bot(command_prefix= prefix)
big_var = {}

@bot.command(name='func1')
@commands.max_concurrency(1, wait = True)
async def func1(ctx):
    func1code(big_var)

bot.run(TOKEN)

I want to run a function clear_data(big_var) if the last use of big_var was X minutes ago, in order to save memory.如果上次使用big_var是 X 分钟前,我想运行 function clear_data(big_var) ,以保存 memory。

I tried:我试过了:

from discord.ext import commands

bot = commands.Bot(command_prefix= prefix)
big_var = {}

@bot.command(name='func1')
@commands.max_concurrency(1, wait = True)
async def func1(ctx):
    func1code(big_var)
    await asyncio.sleep(600)
    clear_data(big_var)

bot.run(TOKEN)

But this prevents the function func1() from finishing and the max_concurrency decorator will only allow 1 instance of func1() to be running at a time.但这会阻止 function func1()完成,并且max_concurrency装饰器一次只允许func1()的 1 个实例运行。

How can I resolve this issue?我该如何解决这个问题?

EDIT: Rewrote question to make it more clear编辑:重写问题以使其更清楚

threading.Timer is a function that calls the second argument after the first argument number of seconds. threading.Timer是一个 function 在第一个参数秒数之后调用第二个参数。 You might do something like:您可能会执行以下操作:

def del_big_var():
    global big_var
    del big_var

t = threading.Timer(X * 60, del_big_var)

def command_that_need_big_var():
    global t
    t.cancel()
    t = threading.Timer(X * 60, del_big_var)

It does not block either.它也不阻塞。

import threading
import time

def foo(*args, **kwargs):
    print("TIME!")

def main():
    t = threading.Timer(3, foo)
    t.start()
    while True:
        print("going")
        time.sleep(1)

main()

The above will produce以上会产生

going
going
going
TIME!
going
going
going
Traceback (most recent call last):
  File "thread_timer.py", line 15, in <module>
    main()
  File "thread_timer.py", line 13, in main
    time.sleep(1)
KeyboardInterrupt

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

相关问题 如何在Python中重复x分钟? - How can I repeat something for x minutes in Python? 5分钟后如何退出Python3脚本 - How can I exit a Python3 script after 5 minutes 如何在x分钟后重复停止程序并在python中更改一个变量来重新运行该程序? - How to repeatedly stop a program after x minutes and re run it changing one variable in python? 如果频道未满,如何在 3 分钟后删除 Discord 上的任何语音频道? - How can I delete any voice channel on Discord after 3 minutes if the channel is not full? 如何在 Python 中使用 matplotlib 在绘图中的所有 x 值的 xaxis 中以小时和分钟格式显示标签? - How can I show labels in hours and minutes format in xaxis for all x values in a plot using matplotlib in Python? 如何在 x 分钟后暂停此线程 - How do I pause this thread after x minutes 如何避免在 for 循环中声明未使用的变量? - How can I get around declaring an unused variable in a for loop? 如何从 python 中的变量中删除某些单词和数字 - how can i delete certain words and numbers from a variable in python 我如何在特定时间后每 30 分钟增量安排我的代码 Python - How can i schedule my Code in Python at every 30 minutes increment after a particular time 如何让 discord 机器人在不活动 x 分钟后离开语音通道? - How to make a discord bot leave the voice channel after being inactive for x minutes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM