Python:从函数返回值而无需等待完成它

时间:2017-12-15 05:24:03

标签: python-3.x asynchronous background-process function-calls

我是python的新手,所以不确定如何实现这个但我想运行一个函数,它将返回一些虚拟值而无需等待完成它,函数必须异步完成任务。

像:

msg = {
     "status": "Process is initiated."
}

def b():
    print ("3")
    time.sleep(5)
    fh = open("hello.txt","w")
    fh.write("Hello World")
    print ("4")
    fh.close()

def a():
    print ("1")
    x = b()
    print (x)
    print ("2")

if __name__ == '__main__':
    a()


Desired Output:
    1
    {"status": Process is Initiated."}
    2

and function b should work in background and finish it work asynchronously. In this case After 5 seconds it should write into the file.

0 个答案:

没有答案