Chilkat Forum

General => General Discussion => Topic started by: Chilkat on May 07, 2018, 07:27:38 AM

Title: Multiple Asynchronous REST API Tasks with One Connection?
Post by: Chilkat on May 07, 2018, 07:27:38 AM
What happens if I have one CkRest connection and generate two or more FullRequestNoBodyAsync CkTask's, and then start them all together with Run?

Will they be processed one after another in a queue?
Title: Re: Multiple Asynchronous REST API Tasks with One Connection?
Post by: Chilkat on May 07, 2018, 07:46:49 AM
I'll begin answering this question by describing the Chilkat thread safety, which will help in understanding the behavior.

Every Chilkat class is thread-safe in the following way:  If your application has multiple threads, and if each thread simultaneously calls the synchronousFullRequestNoBody method (or any method on the same object instance)  then only one thread at a time is allowed inside a method of a particular object.  The behavior is that one thread's call will run and all others will be blocked.  When the method call returns, another thread's call is allowed to pass, and so on.  All method calls will occur one after the other, but the order in which they are called is random.

Now consider the case where your application has one thread, and it calls the asyncrhonous FullRequestNoBodyAsync method (all on the same object instance), and calls Run for each.   The CkGlobal.MaxThreads property controls the max size of Chilkat's async (background) thread pool.  Assuming this is larger than the number of tasks started by calling Run, then all tasks will begin simultaneously.  However, now we are in the situation equivalent to what I described above.  When you call Run, it is the synchronous FullRequestNoBody method that is called from the background thread (managed by Chilkat).  Thus you have N threads each calling the synchronous Chilkat method all trying to gain access to the same object instance, and each will run one after the other but in a random order.

If you wish to run all HTTP requests simultaneously, then you would need each N CkRest objects, each with its own connection.  If you called Run for each, then you don't have the situation where N threads are tryinig to gain access to a single object.  Each thread has its own object and they all operate independently and simultaneously.

If you wish to run N HTTP requests, one after the other in a particular order using the same CkRest object instance, then you can do so using the CkTaskChain object.  (See the online reference documentation.)