python 一个简单的装饰函数,你想在主线程之外的背景中抛出。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 一个简单的装饰函数,你想在主线程之外的背景中抛出。相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: codykochmann
# @Date:   2017-02-04 08:10:05
# @Last Modified 2017-02-05
# @Last Modified time: 2017-02-05 07:46:49

""" A simple decorator for functions that you wanna throw in the
    background outside of the main thread
"""

from threading import Timer
import wrapt

@wrapt.decorator
def background(wrapped, instance, args, kwargs):
    """ decorator that makes the function run in its own thread
        in order to collect the results of each thread, just have
        it append its output to a global list or dict
    """
    # 0.000001 is how many seconds to wait until it starts the new thread
    new_thread = Timer(0.000001, wrapped, args, kwargs)
    new_thread.start()
    return

以上是关于python 一个简单的装饰函数,你想在主线程之外的背景中抛出。的主要内容,如果未能解决你的问题,请参考以下文章