Python question regarding a server listener

multithreadingpluginspythonsleepteamcity

I wrote a plug-in for the jetbrains tool teamcity. It is pretty much just a server listener that listens for a build being triggered and outputs some text files with information about different builds like what triggered it, how many changes there where etc etc. After I finished that I wrote a python script that could input info into teamcity while the server is running and kick of a build. I would like to be able to get the artifacts for that build after the build is ran, but the problem is I don't know how long it takes each build to run. Sometimes it is 30 sec other times 30 minutes.

So I am kicking off the build with this line in python.

    urllib.urlopen('http://'+username+':'+password+'@localhost/httpAuth/action.html?add2Queue='+btid+'&system.name=<btid>&system.value=<'+btid+'>&system.name=<buildNumber>&system.value=<'+buildNumber+'>')

After the build runs (some indetermined amount of time) I would like to use this line to get my text file.

urllib.urlopen('http://'+username+':'+password+'@localhost/httpAuth/action.html?add2Queue='+btid+'&system.name=<btid>&system.value=<'+btid+'>&system.name=<buildNumber>&system.value=<'+buildNumber+'>')

Again the problem is I don't know how long to wait before executing the second line. Usually in Java I would do a second thread of sorts that sleeps for a certain amount of time and waits for the build to be done. I am not sure how to do this in python. So if anyone has an idea of either how to do this in python OR a better way to do this I would appreciate it. If I need to explain myself better please let me know.

Grant-

Best Answer

Unless you get get notified by having the build server contact you, the only way to do it is to poll. You can either spawn a thread as indicated in other comments, you just have your main script sleep and poll.

Something like:

wait=True
while wait:
   url=urllib.urlopen('http://'+username+':'+password+'@localhost/httpAuth/action.html?add2Queue='+btid+'&system.name=<btid>&system.value=<'+btid+'>&system.name=<buildNumber>&system.value=<'+buildNumber+'>')
   if url.getcode()!=404:
     wait=False
   else:
     time.sleep(60)

As an alternative, you could use CherryPy. Then, when the build is done, you could have curl or wget connect to the listening CherryPy server and trigger your app to download the url.

You could also use xmlrpclib to do something similar.