I've an application which receives and sends data (JSON) from/to a HTTP server via HTTP POST requests.
There is not really any payload except of a few strings, so I'm wondering if it would make sense to build this whole HTTP communication as an Android Service or just to create a separate thread within my application?
A thread should be fine unless you need to download data when your application won't be on screen.
Related
I have an Android client app that sends some data to a server in Python, where the Python server is supposed to run a long time-consuming operation/computation and return the results to the client.
To do so, I initially started using Flask with Python on the server side, and an asynchronous android http library on the client side to send the data via http POST. However, I quickly noticed that this is not the way to go, because the computation on the server takes time which causes problems such as the client getting timeout errors ... etc.
Then, I started using Tornado's Websockets on the server side, and an android library for websockets on the client side. However, the first main problem is that when the server is running the time-consuming operation for a given client, the other potential clients need to wait ... and it seems a bit of a pain to make tornado work in a multi-threaded setting (as it is originally planned to be single-threaded). Another minor problem, is if the client goes off-line while the server is processing his request, then the client might never get the result when he connects back.
Therefore, I would like to ask if you have any solutions or recommendation on what to use if I want to have such a setting with an asynchronous multi-threaded Python server who is supposed to do heavy-cpu computations with data from a client without making the other potential clients wait for their turn; and potentially making the client able to get the result from the server when he connects back.
FIrst of all, if you're going to do cpu-heavy operations in your backend, you [most probably] need to run it in separate process. Not in thread/coro/etc. The reason is that python is limited to single thread at time (you may read more about GIL). Doing cpu-heavy operation in multithreading gives your backend some availability, but hits performance overall.
Simple/old solution for this — run your backend in multiple process (and threads, preferably). I.e. deploy your flask with gunicorn, give it multiple worker processes. This way, you'll have system that capable of doing number_of_processes - 1 heavy computations and still be available for handling requests. Limit for processes is usually up to cpu_cores * 2, depending on cpu arch.
Slightly more complicated:
accept data
run heavy function in different process
gather result, return
Great interface for this would be ProcessPoolExecutor. The drawback is — it's harder to handle failures/process hanging over
Another way around is task queue + workers. One of most used is celery. Idea is to
open WS connection
put task in queue
worker (in different process or even different physical node) eventually picks up task, compute it, put result in some DB
main process gets callback/result of long polling over result DB
main process sends result over WS
This is more suited for really heavy and not real-time tasks, but gives you out-of-the-box handling for failures/restarts/etc.
I am a web developer and writing services for android. The android developer, whom with I am working, is sending multiple async calls to the server, and asking me to send a service code with in the response of each service, as he will able to decide about a particular response belongs to which service that he sent before.
Is'nt there a way in android to keep track the responses of multiple async calls? Why should the server tell to android that which service is served.
There is no need for the server to send any code . He could send each http request using IntentService with a key. On each response from your server, he puts the key in a bundle, compare it and do what he wants.
See this code : https://github.com/JCERTIFLab/jcertif-android-2013/blob/master/src/com/jcertif/android/fragments/InitialisationFragment.java
I think their is no need of service code to keep track of the response-request pair, because if 5 threads sends the 5 different request, then, all of them receive their responses only.
Order of retrieval of response may vary.
Lets assume a condition,
ThreadA sends requestA.
ThreadB sends requestB.
Then,
ThreadA receives responseA only.
ThreadB receives responseB only.
Order variation:
ThreadB response may come before ThreadA.
I have a nanoHTTPD server running on my app in background, when i have like more than 30 http get requests to the server, from then server getting stopped, image loading fails.
is there a limit for number of http request in server, or is the android cannot handle those many request at once ? any help is appreciated.
How is your Android application structured? Is the server started by an activity as part of its normal lifecycle? If so, Android might simply be stopping & garbage collecting your activity once it's in the background.
To avoid that happening you will want to start an Android service and start/stop your NanoHttpd server in the service rather than the application.
There are no limits on the number of pages, or bytes, that the NanoHttpd server can serve. I'd point the finger at the Android lifecycle (without knowing any more that is).
I am trying to do the following and am looking into other people's experiences:
I want to have an HTTP server running on Android that receives GET requests from a client. From there, the server must deliver the query parameters to a Service that will do some processing (sometimes quite heavy). When the service is done, it sends back the data to the server that will then return the response.
Of course, the startService(intent) call is asynchronous so I'm wondering how to tell the server to wait for the processing to be done before sending back the response. At the moment, the inter process messaging is done with the Messenger and Message classes.
What sort of design would be able to achieve that?
Use a bound service (one that implements onBind()) and a transact call on the returned IBinder object. Or write your own AIDL to describe the service's synchronous interface.
You want to bind to your service on app startup, before the HTTP requests start coming, since the bind operation itself is async.
By the way, why does the functionality have to be in the service? If the service is in the same app, can you move it directly to the HTTP server class?
I have a web service which has two different methods like 'returnInt' and 'returnImg'.
Is it possible to call these two methods in my single android activity?
I am using kSOAP for this and I have a java WebService.
Well in my application;
I have a Python XML WebService on the server side. On the client side I use Android-xmlrpc library which is a very thin and open source library and works great for my case.
In the client application, it is possible to call more than one method of the WebService within the same activity. But I suggest to, issue webService calls on the separate threads. Also be sure that your web server is multi-threaded and capable of concurrent calls.
yes its possible to call any number of WS from an acitivty. Just make sure that each WS call is running in a different thread and if one WS call needs to precede the other then proper handling using message queues is done.