保存和检索线程对象/状态python

时间:2018-03-09 20:33:00

标签: python multithreading python-multithreading

我第一次使用python线程模块,这是我想要找到的 - 我们可以在某些数据库或文件中保存和加载python线程对象的状态吗?让我们说我在程序中有下面的代码(这个程序像使用python twisted的服务器一样运行)来异步执行某些操作(比如以非阻塞方式复制文件)。

def foo():
    print('Hello world!')

import threading
thr = threading.Thread(target=foo, args=(), kwargs={})
thr.start()

现在,我不会在这里做thr.join(),因为我不想等待线程完成。现在,是否可以保存线程或对象的状态,然后检索它并查明线程是否仍然存活?我可以通过执行此操作获取线程ID和状态,但只能通过线程对象。

# thr.ident
# check status of thread by doing thr.is_alive()

我在这里可能完全错了 - 但是可以保存/加载线程对象吗?另外,寻找建议。非常感谢!

1 个答案:

答案 0 :(得分:0)

  

是否可以保存/加载线程对象?

不,不是。