views:

100

answers:

1

I am writing an update application in Python 2.x. I have one thread (ticket_server) sitting on a database (CouchDB) url in longpoll mode. Update requests are dumped into this database from an outside application. When a change comes, ticket_server triggers a worker thread (update_manager). The heavy lifting is done in this update_manager thread. There will be telnet connections and ftp uploads performed. So it is of highest importance that this process not be interrupted.

My question is, is it safe to spawn update_manager threads from the ticket_server threads?

The other option might be to put requests into a queue, and have another function wait for a ticket to enter the queue and then pass the request off to an update_manager thread. But, Id rather keeps tings simple (Im assuming the ticket_server spawning update_manager is simple) until I have a reason to expand.

# Here is the heavy lifter
class Update_Manager(threading.Thread):
    def __init__(self)
        threading.Thread.__init__(self, ticket, telnet_ip, ftp_ip)
        self.ticket = ticket   
        self.telnet_ip = telnet_ip
        self.ftp_ip = ftp_ip


    def run(self):
        # This will be a very lengthy process.
        self.do_some_telnet()
        self.do_some_ftp()

    def do_some_telnet(self)
        ...

    def do_some_ftp(self)
        ...

# This guy just passes work orders off to Update_Manager
class Ticket_Server(threading.Thread):
    def __init__(self)
        threading.Thread.__init__(self, database_ip)
        self.database_ip

    def run(self):
        # This function call will block this thread only.
        ticket = self.get_ticket(database_ip)

        # Here is where I question what to do.
        # Should I 1) call the Update thread right from here...
        up_man = Update_Manager(ticket)
        up_man.start

        # Or should I 2) put the ticket into a queue and let some other function
        # not in this thread fire the Update_Manager. 


    def get_ticket()
    # This function will 'wait' for a ticket to get posted. 
    # for those familiar with couchdb:
        url = 'http://' + database_ip:port + '/_changes?feed=longpoll&since=' + update_seq
        response = urllib2.urlopen(url)

This is just a lot of code to ask which approach is the safer/more efficient/more pythonic Im only a few months old with python so these question get my brain stuck in a while loop.

+3  A: 

The main thread of a program is a thread; the only way to spawn a thread is from another thread.

Of course, you need to make sure your blocking thread is releasing the GIL while it waits, or other Python threads won't run. All mature Python database bindings will do this, but I've never heard of couchdb.

Glenn Maynard
couchdb is one of those newfangled "object oriented databases".
Arafangion
@Arafangion, maybe we should check 'What it is Not' <http://couchdb.apache.org/docs/intro.html> before misleading those who havn't heard of couchdb.
sbartell
@Glenn, i should provide an example in a few to better illustrate my question.
sbartell
@sbartell: Point noted - perhaps my terminology was very inaccurate, however, it does strike me as being much the same thing here. Note that I don't specify what /type/ of object-orientation we're talking about, but to me it seems that it would fit prototype-based object-oriented language systems very neatly.
Arafangion
@Glenn, the main program is a thread, got it. I hadn't made that connection before. So, in this 'main thread' I will be starting another thread, the ticket_server that waits on a url (which is dependent on the connection remaining alive) for changes. Is it safe to fire the Update_Manager threads from inside this thread? OR, is the more accepted approach to pass them into a queue and have another method/thread (that does not rely on thread_server remaining connected) pass the tickets off to the Update_manager threads?
sbartell
Like, what if the ticket_server connection fatally dies? In that case we have god knows how many Update_Manager threads hanging out in the dark. Is this a correct train of thought or am i over-thinking?
sbartell
If the connection dies and you close the thread, you need to clean up any resources (like other threads) that depend on the thread as well. The same should be done if a thread exits due to an exception. This applies to the main thread just as much as other threads. (It's OK to start threads that *don't* depend on the thread launching it, essentially passing off ownership of the new thread to a different manager thread, too.)
Glenn Maynard