English 中文(简体)
单一函数呼叫导致连续返回
原标题:Return continuous result from a single function call

I have got stuck with a problem. It goes like this, A function returns a single result normally. What I want is it to return continuous streams of result for a certain time frame(optional).

一项职能是否可行,即反复将结果反馈给一个职能电话?

在浏览网络时,我确实碰到了点心和线条。如果这样,它会有效吗?如果有谁想到怎么解决它呢?

我只是需要调用该职能 执行工作 并返回结果 之后,每一任务 完成。

问题回答

为什么问题里没有具体说明你需要这个, 所以很难知道你需要什么, 但我会给你一个一般的想法, 和代码。

您可以这样返回: return var1, var2, var3 (但这不是你需要的)

您有多个选项: 屏蔽或非屏蔽。 屏蔽意味着您调用此函数时您的代码将不再执行。 不屏蔽意味着它将平行运行。 您也应该知道您绝对需要修改调用该函数的代码 。

如果你们欲在一条线上加一条线,

def your_function(callback):
    # This is a function defined inside of it, just for convenience, it can be any function.
    def what_it_is_doing(callback):
        import time
        total = 0
        while True:
            time.sleep(1)
            total += 1
            # Here it is a callback function, but if you are using a
            # GUI application (not only) for example (wx, Qt, GTK, ...) they usually have
            # events/signals, you should be using this system.
            callback(time_spent=total)

    import thread
    thread.start_new_thread(what_it_is_doing, tuple(callback))

# The way you would use it:
def what_I_want_to_do_with_each_bit_of_result(time_spent):
    print "Time is:", time_spent

your_function(what_I_want_to_do_with_each_bit_of_result)
# Continue your code normally

另一个选项(阻塞)涉及一种特殊功能 < code> generators ,这些功能在技术上被作为迭代器处理。 因此,您将它定义为函数,并充当迭代器。 例如,使用与其他函数相同的假函数 :

def my_generator():
    import time
    total = 0
    while True:
        time.sleep(1)
        total += 1
        yield total

# And here s how you use it:
# You need it to be in a loop !!
for time_spent in my_generator():
    print "Time spent is:", time_spent

# Or, you could use it that way, and call .next() manually:
my_gen = my_generator()
# When you need something from it:
time_spent = my_gen.next()

note 在第二个示例中,代码没有意义,因为代码没有在1秒间隔内真正被调用,因为每次调用 yeld 时,都有另一个代码运行。 下一步的 可能需要时间。 但我希望你明白这一点。

再次,它取决于您正在做什么, 如果您正在使用的应用程序有“ 活动” 框架或类似的框架, 您需要使用这个框架, 如果您需要它屏蔽/ 不阻塞, 如果时间很重要, 您的调用代码应该如何操纵结果...

您的活动和线条在正确的轨道上, 因为函数会做它被编程要做的事, 要么一次接受 1 var, 要么选择一个设置, 然后返回一个套或一个 var 。 函数必须被调回结果中, 并且连续的处理流可能已经在进行中, 或者您正在询问一个环绕在内核指示器上或类似的东西, 而您不是, 所以...

所以,您的“ 强” 调用代码包含您函数 < / 强” 很重要, 函数, 任何函数, 例如, 甚至一个真实/ 假布尔函数, 只能执行到用它的 vars 完成, 所以在您的情况中, 调用函数会无限期地监听 。 如果它不存在, 您应该写一个 ;)

封装的调用密码当然非常重要

人们不会有足够的信息帮助很多, 除了超通用的意义上的, 我们可以告诉你,你正在或应该 在一个框架事件循环中, 或其他代码循环中 已经某种形式的 - 这就是你想要 监听/准备数据的目的。

我喜欢"功能编程s","映射功能"之类的东西。我想。我不能在我的代表级别上发表评论,或者我将我的猜测限制在这一点上。 ) (掌声) : (掌声)

如果可能的话,从另一个人那里得到更好的答案 张贴一些示例代码 并披露你的API。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签