0

I have been writing some a django project / app that initiates some test scripts. I previously asked a question about how to do this here.. integrating python scripts with django

I've hit a problem which I didn't think would happen. I have a form working, using jquery to display certain checkboxes that pass values which initiates an api to my application I am testing. Exactly like the code in my initial question. However, when I submit the form, the form hangs whilst the back end process finishes

submittest.initialise_test(t_start, t_templatename, t_datacenter)

return render_to_response('testsubmitted.html')

I know I could use a popen or cron, however I have written my app and it would take some re-engineering.

Any ideas how I could make the submittest.initialise_test(t_start, t_templatename, t_datacenter) an asynchronous call or a way to get round this issue?. The results get written to a database, which I was then hoping to use to render to a results page using ajax to update the results.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Oli
  • 2,267
  • 5
  • 22
  • 21
  • You can try spawning new thread http://docs.python.org/library/thread.html#module-thread – Rohan Aug 29 '12 at 09:23
  • Thanks - I've been reading up on this.. I just don;t want any spawned threads that are left open but I will definitely consider this.. could be an easy option – Oli Aug 29 '12 at 09:35
  • Actually this worked and I didn't use a message queue in the end. All I did was import thread, then change the submittest.intialise_test(t_start, t_templatename, t_datacenter) to thread.start_new_thread(submittest.intialise_test, (t_start, t_templatename, t_datacenter)) – Oli Aug 29 '12 at 11:58
  • glad! that seemed easiest approach if you don't want to monitor,manipulate the thread/task. – Rohan Aug 29 '12 at 12:04

1 Answers1

1

You can use one of the many django background queue apps to run a task in a background process.

jpic
  • 32,891
  • 5
  • 112
  • 113
  • yeah I thought of that. I'll have a look and see what options there are.. I have a feeling it will be a big rewrite of the app though – Oli Aug 29 '12 at 09:31
  • Well I don't know your code so I can't say ... but if it's clean then you should find a quick way. Don't try to put much code in task definitions, rather, try to make tasks call code that's properly factored out in your app (so that you can use that code both with and without a task). – jpic Aug 29 '12 at 09:33
  • sure - thanks for the advice. My app is around 7000 lines of code. I have used classes to define different functions and have split these classes up in apps in django. Ie, api calls in one app, forms and views in another app, db calls in another app. But to summarise all these calls in to a question is fairly hard to do. However, the calls to the apis is where I need initiate the tasks so maybe its not going to be that bad after all. I'll just place these tasks in a queue an poll for the result.. Well that's the theory – Oli Aug 29 '12 at 09:40
  • This is what I am going to use.. http://huey.readthedocs.org/en/latest/getting-started.html. If I struggle, i will repost a new question.. Thanks! – Oli Aug 29 '12 at 09:47