来自计划事件的 Python 返回值 [英] Python return value from scheduled event

查看:47
本文介绍了来自计划事件的 Python 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取从预定事件调用的函数返回的值.以下是我想做的一个简单示例.

I would like to get the value returned from a function called by a scheduled event. Below is a simple example of what I would like to do.

import time
import sched

schedule = sched.scheduler(time.time, time.sleep)

def ret_this():
    return "This."

def schedthis():
    schedule.enter(2,1,ret_this, ())
    schedule.run()
    ## <- print returned value here

if __name__ == "__main__":
    schedthis()

我尝试了很多事情,例如:

I have tried many things like:

     schedule.enter(2,1,print ret_this, ())

甚至:

    schedule.enter(2,1,ret = ret_this, ())
    schedule.run()
    print ret


也许需要一些背景知识.

我想创建一个将侦听传入连接的服务器(已完成.).当建立连接时,将产生一个新线程,并将套接字解析为该线程.在那个线程上,发送到服务器的值将被读取,服务器将判断客户端试图安排什么.一旦服务器知道客户端想要什么,它就会调度它.

如果用户想在预定事件完成后查看信息(这个答案出现的地方),那么它会等到事件结束并将返回的值发送给客户端.

如果不是,则将安排该事件,并且客户端将收到完成,告别".并且连接将关闭.

我称之为编程之神,请你帮帮我.


Maybe some background will be necessary.

I would like to create a server that will listen for incoming connections(This is done.). When a connection is established a new thread will spawn and the socket will be parsed to that thread. On that thread the values sent to the server will be read and the server will judge what the client is trying to schedule. Once the server knows what the client wants it will schedule it.

If the user wants to view the information after the scheduled event has completed (Where this answer comes in) then it will wait until the event has ended and send the returned values to the client.

If not the the event will be scheduled and the client will receive "Done, farewell." and the connection will close.

I call the programming gods, please could you help me.

推荐答案

问题其实很简单.你从 ret_this 函数返回了一些东西,但你没有把它放在任何地方,也没有打印它!

The problem is actually prety simple. You're returning something from the ret_this function, but you didn't put it anywhere, nor printing it!

编辑
请看这个

事实证明,我之前发布的代码只执行函数,而不是在调度程序运行时执行.由于 scheduler.run 只运行一个函数而不实际关心它返回的值,你必须将它存储在某个地方才能真正知道它何时被调用.

Turns out that the code i posted earlier only executes the function, not when it's run by the scheduler. Since scheduler.run only runs a function without actually care about the value it returns, you'll have to store it somewhere to actually know when it's called.

例如,在一个全局变量中:

For example, in a global variable:

def ret_this():
    global result
    result = "I'm called!"

或者直接从 ret_this 打印出来:

Or just print it from ret_this:

def ret_this():
    print "This."

编辑 2
另一种存储它的方法是将其写入文件.您可以稍后再阅读:

Edit 2
Another way to store it is writing it in a file. You can read it again later:

def ret_this():
    with open('output.txt', 'w') as f:
        f.write('I am called!')

if __name__ = "__main__":
    schedthis()
    with open("output.txt", 'r') as f:
        print(f.read())

如果执行成功,您的文件将包含I am call!.否则,它将为空.

If the execution was succesful, your file will contain I am called!. Else, it will be empty.

希望这会有所帮助!

这篇关于来自计划事件的 Python 返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆