(hatena (diary ’Nobuhisa)) このページをアンテナに追加 RSSフィード Twitter

16/10/06 :

[][]ワーカープロセスとstatic変数に関してのメモ

asp.net - IIS app pools, worker processes, app domains - Stack Overflowより:

In a server you can have many asp.net sites that runs together. Each one site is an app domain.


You must assign to each of them one application pool. Many application domains (sites) can have the same application pool, and because they have the same application pool they run under the same processes, and under the same account - and they have the same settings of the pool. If this pool restarts, then all sites under that pools restarts.


Now each pool can have one or more worker process. Each worker process is a different program that's run your site, have their alone static variables, they different start stop calls etc. Different worker process are not communicate together, and the only way to exchange data is from common files or a common database. If you have more than one worker process and one of them make long time calculations, then the other can take care to handle the internet calls and show content.


When you assign many worker process to a single pool then you make the called web garden and your site is like to be run from more than one computer if a computer is one processing machine.


Each worker process can have many threads.


How the more worker process affect you:

When you have one worker process everything is more simple, among your application all static variables are the same, and you use the lock to synchronize them.

When you assign more than one worker process then you still continue to use the lock for static variables, static variables are not different among the many runs of your site, and if you have some common resource (e.g. the creation of a thumbnail on the disk) then you need to synchronize your worker process with Mutex.


RazorEngineでstatic変数を当たり前のように使用していたので、ASP.NET(MVC)に乗せた際にどうなるのだろうと思って調べていました。

static変数はワーカープロセス内で共通なのですね。安易に使うのはちょっとこわい。

そういえば、HttpContext.Currentもstatic変数(プロパティ)だけど、あれは一体どういう扱いになってるんだろう。プロパティがstaticだというだけで、値が使い回されるわけではないですよね。(たぶん)


ASP.NETの内部についてもっと勉強しないとなぁ。

Web上で拾える情報に少し限界を感じたので、やはり書籍が必要になりそう。