1

I am using the python threading module for the first time and this is what I am trying to find out -- can we save and load state of python thread object in some database or file? Let's say I have the below piece of code in a program (this program runs like a server using python twisted) to do something asynchronously (like copying files in a non-blocking fashion).

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

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

Now, I don't do thr.join() here as I don't want to wait for the thread to complete. Now, is it even possible to save the state of thread or object and later retrieve it and find out if the thread is still alive or not? I can get the thread id and status by doing this but only through the thread object.

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

I may be completely wrong here - but is it possible to save/load thread objects? Also, looking for suggestions. Thanks a lot!

yguw
  • 856
  • 6
  • 12
  • 32
  • I think its a strange question, you should probably explain higher level what your goal is, because threads might not be the answer. The `thr` object will stick around as long as your program runs unless something happens to end it. There'd be no point saving it to a file or database because you will always be able to call methods on `thr` as long as it runs. If you don't have a reference to it then that means it isn't running. There's a bunch of suggestions here: https://stackoverflow.com/questions/1239035/asynchronous-method-call-in-python – Davos Mar 12 '18 at 09:52

1 Answers1

2

is it possible to save/load thread objects?

No, it is not.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685