Python 作业完成后运行异步函数
这是一个flask服务器,它还控制一个discord bot。 On_ready()是程序的入口点,然后将其带到my_background_task()。 此函数用于添加每天在特定时间运行的cron作业 我想运行check_Test(),它将根据cron作业是运行fn_1还是fn_2在discord通道中发送适当的消息 我面临的问题是,我不知道如何在调度器中添加的作业完成后立即调用check_Test() 代码如下:Python 作业完成后运行异步函数,python,asynchronous,cron,scheduled-tasks,discord.py,Python,Asynchronous,Cron,Scheduled Tasks,Discord.py,这是一个flask服务器,它还控制一个discord bot。 On_ready()是程序的入口点,然后将其带到my_background_task()。 此函数用于添加每天在特定时间运行的cron作业 我想运行check_Test(),它将根据cron作业是运行fn_1还是fn_2在discord通道中发送适当的消息 我面临的问题是,我不知道如何在调度器中添加的作业完成后立即调用check_Test() 代码如下: import discord import asyncio from disc
import discord
import asyncio
from discord.ext import commands, tasks
import os
import flask
from apscheduler.schedulers.background import BackgroundScheduler
import datetime
server = flask.Flask(__name__)
test = ""
client = discord.Client()
client = commands.Bot(command_prefix = ".")
channel_id = ****
channel = client.get_channel(channel_id)
async def my_background_task():
await client.wait_until_ready()
channel_id = ****
channel = client.get_channel(channel_id)
scheduler = BackgroundScheduler(daemon=True)
scheduler.add_job(fn_1, "cron", day_of_week="mon-sat", hour=00, minute=59, second=20, replace_existing=True, id="fn_1")
scheduler.start()
@client.event
async def on_ready():
channel_id = ****
channel = client.get_channel(channel_id)
print("bot ready")
await channel.send('This Bot is now Ready')
client.loop.create_task(my_background_task())
def fn_1():
global test
print("running some code for fn_1")
test = "success1"
def fn_2():
global test
print("running some code for fn_2")
test = "success2"
async def check_Test():
if(test == "success1"):
channel_id = ******
channel = client.get_channel(channel_id)
await channel.send('Test Has value of : Success 1')
elif(test == "success2"):
channel_id = ****
channel = client.get_channel(channel_id)
await channel.send('Test Has value of : Success 2')
client.run("****")
为此,我认为应该将check_Test()设置为一个任务循环()
或者,您应该将fn_1()和fn_2()设置为任务循环,然后在调用check_Test()的方法的末尾。为此,我认为您应该将check_Test()设置为任务循环() 或者您应该将fn_1()和fn_2()设置为tasks循环,然后在调用check_Test()的方法的末尾
@tasks.loop(hours=1)
async def check_Test():