简体   繁体   English

Python线程类对象并通过pickle存储

[英]Python thread class object and storing via pickle

Within Python, I have been using a thread to execute a pipeline from my main script:在 Python 中,我一直在使用线程从我的主脚本执行管道:

thread = PipeLine(input)
thread.daemon = True
thread.start()
result = thread.join()

My class object in the other script looks this:我在另一个脚本中的类对象如下所示:

class PipeLine(threading.Thread):
    def __init__(self, input=()):
...

I call this code within a GUI, so using a Daemon thread seemed like a good idea to keep the GUI operative, while the process is busy.我在 GUI 中调用此代码,因此在进程繁忙时使用守护进程线程似乎是保持 GUI 运行的好主意。 However, I also want to store the Pipeline class object somehow with pickle after the thread has finished.但是,我还想在线程完成后以某种方式用 pickle 存储 Pipeline 类对象。 This is the code I used before.这是我之前使用的代码。

pipeline = PipeLine(input)
with open(PATHOUT+subj_name+"\\PIPELINE", "wb" ) as f:
    pickle.dump(pipeline, f)  

How do I achieve this in Python 3 without changing what I have too much?如何在 Python 3 中实现这一点而不改变我拥有的太多?

Thanks谢谢

First, a note or two.首先,一两个笔记。 In your code you have:在您的代码中,您有:

thread = PipeLine(input)
                thread.daemon = True
                thread.start()
                result = thread.join()
  1. The indentation is clearly off.缩进明显关闭。
  2. result = thread.join() -> method join() always returns None . result = thread.join() -> 方法 join() 总是返回None What type of result are you looking for?您在寻找什么类型的结果? And why are you calling join() on a daemon thread?为什么要在守护线程上调用join() The whole point of a daemon thread is that the program exists when there are no non-daemon threads left and so daemon threads do not have to be terminated for a program to be exited.守护线程的全部意义在于,当没有非守护线程剩余时,程序就存在,因此不必为了退出程序而终止守护线程。
  3. The name of your argument is input .您的参数名称是input You should try to avoid using names that are builtin function names.您应该尽量避免使用内置函数名称的名称。

Since it is a daemon thread, the intention is for it to run until program termination.因为它是一个守护线程,所以目的是让它一直运行到程序终止。 But the thread is never "finished" in the normal sense when it is a daemon thread;但是当线程是守护线程时,它永远不会在正常意义上“完成”; it runs until there are no non-daemon threads left and then it is killed.它会一直运行,直到没有非守护进程线程为止,然后它就会被杀死。 Resources it held are not released and its current state is somewhat indeterminate.它持有的资源不会被释放,它的当前状态在某种程度上是不确定的。 There is no way to be storing the daemon's thread's state at this point.此时无法存储守护进程的线程状态。 One would think it might be possible to add to the Pipeline class a __del__ destructor that would do the pickling operation when the thread is garbage collected, but that garbage collection never happens for a daemon thread.有人会认为有可能在Pipeline类中添加一个__del__析构函数,当线程被垃圾收集时,它会执行酸洗操作,但垃圾收集永远不会发生在守护线程中。

If you want to pickle the Pipeline object after it terminates, it will have to be a non-daemon thread that is joined at termination of the main thread.如果您想在Pipeline对象终止后对其进行pickle,则它必须是在主线程终止时加入的非守护线程。 You will need to give the Pipeline thread the ability to check for a "stop condition" so that it will return from its run method in some consistent state such that a join call from the main thread will not hang.您需要赋予Pipeline线程检查“停止条件”的能力,以便它以某种一致的状态从其run方法返回,这样来自主线程的join调用就不会挂起。

Thanks for your helpful remarks.感谢您的有益评论。 I solved the case by simply calling making the dump part of a function and then calling the function as a target.我通过简单地调用使转储成为函数的一部分然后将该函数作为目标调用来解决这个问题。 This way, the data is already dumped within the start procedure of the main function.这样,数据就已经在主函数的启动过程中转储了。 Also noted your comment on using input, I will modify that.还注意到您对使用输入的评论,我会修改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM