c# - async and await code behaving differently if we run it from different processes -
please bear me long question.
scenario: have filesystemwatcher
on root-directory watching lastwrite
change on control-file each inside control-directory. looks this:-
root-directory (
filesystemwatcher
instance watching root directory)|
|-----control-directory_0 \ control-file
\ data-file
|-----control-directory_1 \ control-file
\ data-file
|-----control-directory_2 \ control-file
\ data-file
|-----control-directory_3 \ control-file
\ data-file
.........more similar structure (but 1 root-directory)
sorry creative drawing.
the problem facing, there can many control-directories inside root-directory (approx 200-500) , along control-directory's control-file, have data-file in (in each of them) continuous writes happening.
i watching notifyfilter.lastwrite
, filter = control-file
. internalbuffersize
set 4kb (please don't ask increase this, not allowed modify , event make 8kb (default one) due requirements , can't use filesystemwatcher
each control-file take lot of precious non-paged memory).
my eventhandler
looks this
private void onchange(object sender, filesystemeventargs eventargs) { //result = cpu bound work here //evaluating result }
currently, handling 300 control-files. due lots of writes happening on data-file , lots of requests coming control-file buffer overflowing quite regularly.
then thought of idea. (async
, await
rescue task
battle tank)
async private void onchange(object sender, filesystemeventargs eventargs) { var result = await task.run(() => work.cpuboundwork()); //evaluating result }
it working fine , able handle lots of requests (even tested 1000 events 4kb buffer).
now comes interesting part. how testing it?
there 2 entities involved, one, writing control or data-file , second, handling events generated writes control-file. went , created console app, both of things.
static void main(string[] args) { int controlfilecount = convert.toint32(args[0]); string basepath = args[1]; //createfolderstructure(basepath, controlfilecount); first run. filesystemwatcher basedirectorywatcher = new filesystemwatcher(); setupfilesystemwatcher(basedirectorywatcher, basepath); writetocontrolfilesparallely(basepath, controlfilecount); console.read(); } public void setupfilesystemwatcher(filesystemwatcher basedirectorywatcher, string path) { //setting properties of watcher. basedirectorywatcher.notifyfilter = notifyfilter.lastwrite; basedirectorywatcher.filter = "control-file"; basedirectorywatcher.internalbuffersize = 4096; basedirectorywatcher.path = path; basedirectorywatcher.includesubdirectories = true; basedirectorywatcher.enableraisingevents = true; } public void writetocontrolfilesparallely(string basepath, int controlfilecount) { parallel.for(0, controlfilecount, (i) => { string filepath = helper.getfilepath(basepath, i); helper.writedata(filepath, "data"); }); }
next, tested 2 console applications:-
first, responsible of writing data various control files parallelly - (writer_app).
second, responsible handling events - (event_handling_app).
note: there's no code change in core instead of writing , handling events in same console app writing 1 , handling form other.
so separated 2 entities old console app (starting simulation actual environment). started testing running first event_handling_app , write control-file running writer_app , things have become strange , application facing internalbufferoverflow
exception same number of control-files , same number of writes when didn't have async
, await
in first implementation.
i again tested 1 console app (which having both responsibilities) , working fine.
so, why 2 console applications make magic of
async
,await
(alongtask
) disappear while 1 console application working great?is related inter process communication?
i wondering if work in production because using handler code in web application , other process can write these files on network.
finally, able figure out this, when using single console app running exe (through cmd) when trying 2 console app running app inside visual studio (in release mode) due (with load of visual studio) getting exception , blaming async
, await
.
so, run application exe if dealing code tight.
Comments
Post a Comment