semaphore

Use a Semaphore to fix a Concurrency Issue

断了今生、忘了曾经 提交于 2020-07-10 07:05:40
问题 I recently asked a question: link here regarding the best way to store refresh tokens in my autodesk-forge web application. I am currently storing the refresh token in an SQL database with only one row and column, containing the refresh token. Steps regarding the token are as follows: When a user signs in, a GET method is called to retrieve the latest token from the database. Returndata.php simply connects to the SQL DB and retrieves the row from the table. Get method code: function

Cross Process (and machine) Synchronization (Semaphores)

北城以北 提交于 2020-07-09 06:10:29
问题 Background: My WCF application has to call a service that does not have any concurrency checking in it. It is a service created by a third party and getting them to add concurrency checking may not be possible. I can ensure that the only way to call the third party service is via my WCF application. So I am considering putting concurrency checking in my code. To do this, I will use an identifier that uniquely describes the data being modified. The idea is that if one call to the service is

Cross Process (and machine) Synchronization (Semaphores)

南笙酒味 提交于 2020-07-09 06:07:28
问题 Background: My WCF application has to call a service that does not have any concurrency checking in it. It is a service created by a third party and getting them to add concurrency checking may not be possible. I can ensure that the only way to call the third party service is via my WCF application. So I am considering putting concurrency checking in my code. To do this, I will use an identifier that uniquely describes the data being modified. The idea is that if one call to the service is

How to limit number of async IO tasks to database?

安稳与你 提交于 2020-06-11 12:55:55
问题 I have a list of id's and I want to get data for each of those id in parallel from database. My below ExecuteAsync method is called at very high throughput and for each request we have around 500 ids for which I need to extract data. So I have got below code where I am looping around list of ids and making async calls for each of those id in parallel and it works fine. private async Task<List<T>> ExecuteAsync<T>(IList<int> ids, IPollyPolicy policy, Func<CancellationToken, int, Task<T>> mapper

How to limit number of async IO tasks to database?

不羁的心 提交于 2020-06-11 12:53:40
问题 I have a list of id's and I want to get data for each of those id in parallel from database. My below ExecuteAsync method is called at very high throughput and for each request we have around 500 ids for which I need to extract data. So I have got below code where I am looping around list of ids and making async calls for each of those id in parallel and it works fine. private async Task<List<T>> ExecuteAsync<T>(IList<int> ids, IPollyPolicy policy, Func<CancellationToken, int, Task<T>> mapper

How can I measure how many threads are executing a piece of code?

余生颓废 提交于 2020-05-28 18:04:09
问题 I am trying to measure how many threads are executing a section of code at the same time. Currently i am (ab)using Semaphores for this, is there a better way? final int MAX_THREADS = Integer.MAX_VALUE; Semaphore s = new Semaphore(MAX_THREADS); s.acquire(); // start of section // do some computations // track how many threads are running the section trackThreads( (MAX_THREADS - s.availablePermits()) ); s.release(); // end of section 回答1: Use an AtomicInteger instead of a Semaphore . Something

SemaphoreSlim.WaitAsync before/after try block

不羁岁月 提交于 2020-05-28 13:46:48
问题 I know that in the sync world the first snippet is right, but what's about WaitAsync and async/await magic? Please give me some .net internals. await _semaphore.WaitAsync(); try { // todo } finally { _semaphore.Release(); } or try { await _semaphore.WaitAsync(); // todo } finally { _semaphore.Release(); } } 回答1: According to MSDN, SemaphoreSlim.WaitAsync may throw: ObjectDisposedException - If the semaphore has been disposed ArgumentOutOfRangeException - if you choose the overload which

Semaphore Wait vs WaitAsync in an async method

試著忘記壹切 提交于 2020-05-10 04:26:05
问题 I'm trying to find out what is the difference between the SemaphoreSlim use of Wait and WaitAsync, used in this kind of context: private SemaphoreSlim semaphore = new SemaphoreSlim(1); public async Task<string> Get() { // What's the difference between using Wait and WaitAsync here? this.semaphore.Wait(); // await this.semaphore.WaitAsync() string result; try { result = this.GetStringAsync(); } finally { this.semaphore.Release(); } return result; } 回答1: If you have async method - you want to