1

i have a ftp server that uses a serve_forever command. this ftp service is called in a thread ,what i want to do is that i want to abruptly close the thread from the main thread ,when i hit the stop button on my GUI.

import os
import sqlite3
from pyftpdlib import ftpserver
def main():
    authorizer = ftpserver.DummyAuthorizer()
    #does something
    address = ('127.0.0.1', 10221)
    ftpd = ftpserver.FTPServer(address, handler)

    # start ftp server
    ftpd.serve_forever()
if __name__ == '__main__':
    main()

this is my main thread which calls the ftp service

def start_ftp(self):
    self.ftp_status.setText("Running")
    self.ftp_status.setStyleSheet("Background : light green")
    #thread.start_new_thread( FtpService )

def stop_ftp(self):
    self.ftp_status.setText("Stopped")
    self.ftp_status.setStyleSheet("Background : red")
    #what should i put here for the desired result

please help guys

thecreator232
  • 2,145
  • 1
  • 36
  • 51
  • Can you just store `ftpd` somewhere publically visible (ie, in a global) and call `close` or `close_all` from the main thread? – Useless Aug 15 '12 at 11:57
  • @Useless :thanx for the quick response , would you be kind enough to give an example on how to execute the above given answere – thecreator232 Aug 15 '12 at 12:07
  • 2
    You don't want to abruptly close a thread. You want to stop your FTP service. Thinking about the work rather than the execution vehicles is the first step to writing sensible multithreaded code. – David Schwartz Aug 15 '12 at 12:22
  • @DavidSchwartz : any book or material that you would recommend for multi-threaded programming in python – thecreator232 Aug 15 '12 at 12:38

1 Answers1

2

According to the documentation, close_all should "Stop serving ...", so serve_forever should return.

Store your ftpd object in a global variable (currently it is local to main), you can call close or close_all from the main thread.


Per your comment, the steps are:

  1. make ftpd = ftpserver.FTPServer(address, handler) refer to a global variable
  2. call ftpd.close_all() when you want to stop serving ftp connections
Community
  • 1
  • 1
Useless
  • 64,155
  • 6
  • 88
  • 132