如何暂停一个函数?

2021-01-26 00:46:28 +08:00
 yellowtail

代码在 jupyternotebook 里写的,奇怪的是在办公室能用,家里不能用了... 整个功能就是用 apscheduler 定时运行生成随机数然后在 tkinter 里可视化 现在想保留原代码里 root.after 功能(因为直接用 root.after,按现在做法定时函数就没用了,因为 root.after 是循环的) 感觉可以用锁的方法,但是水平捉急写不出来,求指教。。

''' import datetime from apscheduler.schedulers.background import BackgroundScheduler import random import numpy as np from tkinter import * from tkinter import messagebox import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg import multiprocessing from threading import Thread import time

root = Tk()
root.title("tkinter + Matplotlib")
root.geometry('700x750')
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['font.sans-serif'] = ['SimHei'] 
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
ax2 = ax1.twinx()

ax1.set_title('随机生成数')

ax1.set_ylim()
ax2.set_ylim()

ax1.set_xlim(0, 200)
ax2.set_xlim(0, 200)

ax1.grid(True)

ax1.set_xlabel("time")
ax1.set_ylabel("随机数 1")
ax2.set_ylabel("随机数 2")

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()

canvas.get_tk_widget().pack()

x = 0
ran1 = []
ran2 = []
times = []

def drawImg():

    global ran1
    global ran2
    global times
    global x
    global ax1
    global ax2

    v1 = float(random.randint(-5, 5))
    r1 = float(random.randint(-10, 10))
    ran1.append(v1)
    ran2.append(r1)
    times.append(x)
    x += 1

    ax1.clear()
    ax2.clear()
    ax1.axes.set_ylim(min(ran1)-5, 5+max(ran1))
    ax2.axes.set_ylim(min(ran2)-5, 5+max(ran2))
    ax1.axes.set_xlim(x-200+200, x+20)
    ax2.axes.set_xlim(x-200+20, x+20)

    ax1.plot(times, ran1,'y')
    ax2.plot(times, ran2,'r')
    canvas.draw()
    #global afterHandler
    #afterHandler = root.after(10, drawImg)
    #print(datetime.datetime.now())

def sub_task():
    drawImg()

def task_(time):
    def task__():
        temp = BackgroundScheduler()
        temp.add_job(sub_task,'interval',seconds = 1,end_date =str(datetime.datetime.now())[:17]+time)
        temp.start()
    return task__

sche_fn = BackgroundScheduler()
sche_fn.add_job(task_('25'),'cron',day_of_week = 'mon-fri',hour = '*',minute = '*',second = '0' )
sche_fn.start()
sche_an = BackgroundScheduler()
sche_an.add_job(task_('55'),'cron',day_of_week = 'mon-fri',hour = '*',minute = '*',second = '30' )
sche_an.start()



def on_closing():
    sche_an.shutdown()
    sche_fn.shutdown()
    #root.after_cancel(afterHandler)
    answer = messagebox.askokcancel("退出", "确定退出吗?")
    if answer:
        plt.close('all')
        root.destroy()
    else:
        root.after(1000, drawImg)


root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
1341 次点击
所在节点    Python
2 条回复
shniubobo
2021-01-27 09:39:42 +08:00
如果要暂停函数的话可以考虑`yield`。函数里有`yield`的话,调用函数会返回一个生成器。`next(生成器)`会让函数运行到`yield`处中断,并返回`yield`的值,这样就相当于暂停了函数。有关`yield`见 python 文档语言参考部分,`next`见标准库参考部分。
还有就是考虑协程了,见标准库的`asyncio`。python 早期的协程其实也是基于上面说的生成器的。
yellowtail
2021-01-30 17:50:31 +08:00
@shniubobo 感谢回复。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/748341

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX