0

I am getting the following error

Traceback (most recent call last):   File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()   File "python/file_download.py", line 30, in run
    self._downloadFile(host[0], host[1])   File "python/file_download.py", line 57, in _downloadFile
    th.exit() AttributeError: 'Thread' object has no attribute 'exit'

from the code below:

        th = threading.Thread(
                target=self._fileWriteToDisk,
                args=(saveTo, u, file_name),
                name="fileWrite_Child_of_%s" % self.getName(),
                )
        th.setDaemon(False)
        th.start()
        print "Writing to disk using child: %s " % th.name
        th.exit()
Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

1 Answers1

0

There is no need to kill the thread, python threads kill themselves when they are completed.

Abruptly killing a thread is bad practice, you could have resourced that are used by the thread that aren't closed properly. A stop flag is a better way to end a thread, see this for an example.

You should be using the following, instead of hat you have:

th.daemon = False

as th.setDaemon(False) is depreciated.

Community
  • 1
  • 1
Morgoth
  • 4,935
  • 8
  • 40
  • 66
  • Appologies, fixed my answer appropriately – Morgoth Jun 15 '14 at 15:49
  • 1
    A daemon thread is ended when it's parent thread has ended, a non-daemon thread will keep the parent thread alive until the non-daemon is finished. So in your example it look like you are trying to save stuff to the drive? If so you should use a non-daemon thread, if the user clicks close while the thread is still running, then the programme will wait till the save is done, then it will close. Mark answer as solved if I've helped you – Morgoth Jun 15 '14 at 17:19
  • sweet, thanks so I was thinking right by making the write process non-daemon. – Ciasto piekarz Jun 16 '14 at 04:25