(Google App Engine Python 2.7) webapp2 RequestHandler on main thread? - multithreading

I started with a simple Python 2.5 app engine app and migrated it to Python 2.7 in hopes of taking advantage of its multithreaded abilities. After migrating, I noticed that webapp2.RequestHandler instances are all being called from the Main Thread.
I have an AJAX client firing up multiple asynchronous requests. One of the requests I'd like to respond only when certain event occurs on server side. Let's just say that event sleeps for 10 seconds for now. The problem is that sleep occurs in the Main Thread and occupies the thread before processing my second ASYNC request from AJAX. What am I missing?
Here's a stack trace:
PyDevDebug [PyDev Google App Run]
dev_appserver.py
MainThread - pid4276_seq4
post [test1.py:53]
dispatch [webapp2.py:570]
call [webapp2.py:1102]
default_dispatcher [webapp2.py:1278]
call [webapp2.py:1529]
Handle [wsgi.py:223]
HandleRequest [wsgi.py:298]
HandleRequest [runtime.py:151]
ExecutePy27Handler [dev_appserver.py:1525]
ExecuteCGI [dev_appserver.py:1701]
Dispatch [dev_appserver.py:1803]
Dispatch [dev_appserver.py:719]
_Dispatch [dev_appserver.py:2870]
_HandleRequest [dev_appserver.py:3001]
do_POST [dev_appserver.py:2794]
handle_one_request [BaseHTTPServer.py:328]
handle [BaseHTTPServer.py:340]
init [SocketServer.py:638]
init [dev_appserver.py:2780]
finish_request [SocketServer.py:323]
process_request [SocketServer.py:310]
_handle_request_noblock [SocketServer.py:284]
handle_request [dev_appserver.py:3991]
serve_forever [dev_appserver.py:4028]
main [dev_appserver_main.py:721]
[dev_appserver_main.py:747]
run_file [dev_appserver.py:167]
[dev_appserver.py:171]
run [pydevd.py:1090]
[pydevd.py:1397]
Thread-4 - pid4276_seq5
dev_appserver.py

It seems that in the dev (local) server parallel threads are not executed in parallel, but serial.
There is an "experimental dev server" here which you just reminded me of:
An experimental new development server for Google App Engine.
Multithreaded serving for better performance for complex applications and more correct semantics e.g. accessing your own application through urlfetch no longer deadlocks.
That might solve it locally, but I've not tried it personally.

Related

WKWebView load Function in which Thread

I am developing an App with an iOS Deployment target 15.6.
The App uses WKWebview for obtaining url information such as html. When I run a webView.load(url) function in the main thread it works but issues a warning "This method should not be called on the main thread as it may lead to UI unresponsiveness."
Although the function should not be used in a background thread I tried it but of course I got an error message "WKWebView.load(_:) must be used from main thread only".
How can I use the load function correctly so that I receive no warnings?

How can I run a long running blocking function concurrently with tornado IOLoop

I have a tornado application:
if __name__ == "__main__":
app = make_app()
app.listen(8090)
tornado.ioloop.IOLoop.current().start()
How can I have a long running task operate concurrently?
Specifically, I have a redis pubsub which will notify my tornado app of updated authorization tokens.
I tried this:
tornado.ioloop.IOLoop.current().run_in_executor(None, redis_pubsub.subscribe_to_valid_tokens)
Since I never await the Future it doesn't seem to run the function, so i'm a bit stuck about the 'correct' way to do this.
I'm wondering if I should do this with a separate python thread (or maybe leverage the multiprocessing library with a queue?
Is there some better use/structure of async/await that I should use?
I had to store a reference to the redis object and pubsub object in a class that doesn't get garbage collected. Also, the pubsub's run_in_thread method was very helpful!

How to implement an asynchronous function with Rest Api Bundle in Symfony 3.2

I'm still looking for a solution to implement an asynchronous call before returning a response. In other words, I have a long asynchronous process which should start running before returning a response, a user should not be waiting a long time for the end of this process:
$data = ....
...//Here call to an asynchronous function <<----
return $this->getSuccessResponse($data);
I tried with Events, Thread, Process, but no result.
What should I do ? (something expect RabbitMQ)
You can use a queuing system like Beanstalk. With this bundle LeezyPheanstalkBundle you can manage the queues.
In the controller, insert the job in the queue. And, in a command running with supervisor, execute your task.
Edit:
You can use an EventSubscriber

win 8 app multi threading

I have an win8 app and I want to add proggress ring while app gets info from server.
but when
proggressRing.isActivate = true;
checkServer();
the app freezes until returns from checkServer() and proggressRing does not activates when it freezes.
I asked around and said you have to use multi threading
how can I use multi thread in c# or is there any other way?
thanks
There are multiple solutions to this
1) first ensure that CheckServer is implemented as Async Task (Async Event based can still be used).
2) If it is CPU intensive operation, use Task.Run to queue the Task to run on a threadpool thread.
have a look at this post
http://msdn.microsoft.com/en-us/library/windows/apps/hh452713.aspx and this one
http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

http listeners inside threads

I am writing a web service which has to be able to reply to multiple http requests.
From what I understand, I will need to deal with HttpListener.
What is the best method to receive a http request(or better, multiple http requests), translate it and send the results back to the caller? How safe is to use HttpListeners on threads?
Thanks
You typically set up a main thread that accepts connections and passes the request to be handled by either a new thread or a free thread in a thread pool. I'd say you're on the right track though.
You're looking for something similar to:
while (boolProcessRequests)
{
HttpListenerContext context = null;
// this line blocks until a new request arrives
context = listener.GetContext();
Thread T = new Thread((new YourRequestProcessorClass(context)).ExecuteRequest);
T.Start();
}
Edit Detailed Description If you don't have access to a web-server and need to roll your own web-service, you would use the following structure:
One main thread that accepts connections/requests and as soon as they arrive, it passes the connection to a free threat to process. Sort of like the Hostess at a restaurant that passes you to a Waiter/Waitress who will process your request.
In this case, the Hostess (main thread) has a loop:
- Wait at the door for new arrivals
- Find a free table and seat the patrons there and call the waiter to process the request.
- Go back to the door and wait.
In the code above, the requests are packaged inside the HttpListernContext object. Once they arrive, the main thread creates a new thread and a new RequestProcessor class that is initialized with the request data (context). The RequsetProcessor then uses the Response object inside the context object to respond to the request. Obviously you need to create the YourRequestProcessorClass and function like ExecuteRequest to be run by the thread.
I'm not sure what platform you're on, but you can see a .Net example for threading here and for httplistener here.

Resources