小白教程

 找回密码
 立即注册
查看: 6161|回复: 1

[已解决] 任务并在任何给定时间内渲染/使用任务的结果

[复制链接]

5

主题

9

帖子

31

积分

新手上路

Rank: 1

积分
31
发表于 2021-5-18 02:07:46 | 显示全部楼层 |阅读模式
我想计划一个任务,并在计划运行时使用任务的结果。
例如,我想每分钟获取一次当前时间,并将其分配给变量调用“ now4”。因此,“ now4”值应每分钟更新一次。
然后从“ now4”中,我要扣除先前分配的日期时间变量“ custom_date_time”。用户将通过自己运行代码的这一部分来检查这种差异。
因此,用户应该有权访问更新的“ now4”值以获取差异。
  1. from datetime import datetime
  2. import schedule
  3. import time


  4. def my_task():
  5.     now4 = datetime.now()
  6.     print(now4)
  7.     return now4


  8. schedule.every(1).minutes.do(my_task)

  9. custom_date_time = datetime(2021, 5, 1, 17, 30, 29, 431717)



  10. while True:
  11.     schedule.run_pending()
  12.     time.sleep(1)

  13. print(custom_date_time, "Time difference", now4-custom_date_time )
复制代码


本帖寻求最佳方案

 1970-1-1 08:00发表回复被李美林采纳,幸运的获得奖励 C币 5

 详细答案»

回复

使用道具 举报

0

主题

2

帖子

14

积分

新手上路

Rank: 1

积分
14
发表于 2021-5-18 02:08:43 | 显示全部楼层
不确定这是否是您想要的
  1. import schedule, time
  2. from datetime import datetime

  3. def my_task():
  4.     custom_time = datetime(2021, 5, 1, 17, 30, 29, 431717)
  5.     now = datetime.now()
  6.     print(f'Custom Time:{custom_time} Current Time: {now} Time Difference: {custom_time-now}')

  7. schedule.every(1).minutes.do(my_task)

  8. while True:
  9.     schedule.run_pending()
  10.     time.sleep(1)
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 15:06 , Processed in 0.025385 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表