Technically, you can but you will run into problems because the script must
be streamed out to the client thru the response object. The response object
is owned by the main thread and such cross pollination is forbidden. As an
alternative, you can build the script on a thread, pass it back to the main
thread and then the main thread can stream it out to the client.