1

I am trying to determine what will happen when my Web API methods are being called simultaneously by two clients. In order to do so I am generating two async requests in Python:

rs = (grequests.post(url, data = json), grequests.post(url, data = json))
grequests.map(rs)

The requests call a non-async method in my WebApi which is changing my resource, which is a static class instance. From what I've understood, API requests should not run concurrently, which is what I want since I don't want the two requests to interfere with eachother. However, if I set a break point in my API method, the two requests seem to run concurrently (in parallell). This leads to some unwanted results since the two requests are changing the same resource in parallell.

Is there any way to make sure that the Web API handles the requests non-concurrently?

3
  • 1
    Lock your shared resource. Commented Jun 21, 2016 at 14:12
  • @BrianDriscoll Please see my edit. I have made sure that the resource is locked with a queue, however, I am still getting interference between the requests. Commented Jun 21, 2016 at 14:31
  • 3
    Whether or not Web API requests run concurrently is determined by how your API is hosted, it is not determined in code. If you are running in IIS, then your requests will be handled concurrently by separate worker processes. Without knowing more about your API, I would argue that you have a design issue rather than an implementation issue. Commented Jun 21, 2016 at 14:39

1 Answer 1

4

Requests to your Web API methods are handled by the IIS application pool (pool of threads) which initializes a thread for every synchronous request it receives. There is no way to tell IIS to run these threads non-concurrently.

I believe you have a misunderstanding of what a "non-async" Web API method is. When a Web API method is async that means it will share its application pool thread while it's in a wait state. This has the advantage of other requests not having to initialize a new thread (which is somewhat expensive). It also helps minimize the number of concurrent threads which in turn minimizes the number of threads that the application pool has to queue up.

For non-async methods, the IIS application pool will initialize a new thread for every request even if an inactive thread is available. Also, it will not share that thread with any other requests.

So the short answer to your question is no. There is no way to make sure that the Web API requests are handled non-concurrently. Brian Driscoll's comment is correct. You will have to lock your shared resources during a request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.