.net - Scalable actions with async/await -
i'm quite new programming , i'm working on first self-project, it's forum i'm creating asp.net mvc 5 , i'm trying learn it. read lot here , in msdn async actions in mvc have several questions couldn't figure out.
as understood, iis assigns x threads each process, 2 example. , every thread can deal 1 request. when mark action async , wrap return type task, thread assign worker handle request , report iis free handle request. if thread can create 3 workers, action can serve 6 requests. right?
as understood, async method must await async method in order async, if not, method run synchronically. question is, when thread assign worker? when request comes or when awaited method being called?again understood, every async method being assigned worker, if website has 6 workers spare (from above question), amount of workers can handle requests decrease use more , more async methods? example, if have async action calls asynchronically
getuser
, insidegetuser
calls asynchronicallygetuserhistory
, insidegetuserhistory
calls asynchronicallygetlogindata
action "waste" 4 workers have serve requests?if of above true, wouldn't scalable-effective mark action async , make methods in project sync methods , @ end of action (or in beginning) await
task.delay(0)
"waste" 1 worker?
i see questions , think there fundamental didn't understand.
1.as understood, iis assigns x threads each process...
it can more granular that. there machine settings, iis settings, app pool/domain settings.
trying understand thread makes very difficult understand happening. instead think per request. each request uses minimum of 1 thread. in async controller method, if runtime determines there valid opportunity thread context switch (await on io operation example) continuation of request may serived thread.
so when request comes in, thread start fulfiling request. if thread calls io async operation, executereaderaync example) there machine state keeps track of thread doing when async operation occurs. thread becomes clear start fulling request (b). when machine state (or whatever is) determines thread needed continue request (a), thread or other thread takes machine state , continues request.
will amount of workers can handle requests decrease use more , more async methods?
under normal circumstances, no, free threads wait io operation complete service additional requests. adding async method not change threading model of method, denotes runtime may run opportunity "suspend" current operation.
3.if of above true...
it's not, assertion doesn't matter.
why async method call other threads handle request?
an async
method not change how threading works, using await
does.
i mean, async methods trigger new thread
no wrong. neither async
nor await
starts new thread in code.
example:
public async actionresult mymethod() { await doit(); return view(); } public async void doit() { await domore(); } public async void domore() { await task.delay(1000); }
this code use 1 thread. aren't spawnings/using additional threads (maybe threadpool), using machine states release thread in domore()
fulfill other requests. same thread may or may not start , finish request.
update
suppose setup .net application every use single thread. without async when request 1 started , waited 20 seconds finisht request, request 2 wait thread become available (or 500, iis thread starved).
now instead, configure controller , methods make async calls down await somesqlayncmethod()
, thread free response request 2 , return html. once sql call completed, thread reused complete request 1.
Comments
Post a Comment