성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] 그냥 RSS Reader 기능과 약간의 UI 편의성 때문에 사용...
[이종효] 오래된 소프트웨어는 보안 위협이 되기도 합니다. 혹시 어떤 기능...
[정성태] @Keystroke IEEE의 문서를 소개해 주시다니... +_...
[손민수 (Keystroke)] 괜히 듀얼채널 구성할 때 한번에 같은 제품 사라고 하는 것이 아...
[정성태] 전각(Full-width)/반각(Half-width) 기능을 토...
[정성태] Vector에 대한 내용은 없습니다. Vector가 닷넷 BCL...
[orion] 글 읽고 찾아보니 디자인 타임에는 InitializeCompon...
[orion] 연휴 전에 재현 프로젝트 올리자 생각해 놓고 여의치 않아서 못 ...
[정성태] 아래의 글에 정리했으니 참고하세요. C# - Typed D...
[정성태] 간단한 재현 프로젝트라도 있을까요? 저런 식으로 설명만 해...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# 컴파일러 대신 직접 구현하는 비동기(async/await) 코드</h1> <p> C# async/await 코드가 참 마법 같습니다. 단지 예약어 하나 썼을 뿐인데 어떻게 그걸 비동기로 처리해 주는지 신기할 따름인데요. 그저 그 신기함을 누리며 사용하는 것도 좋겠지만, 왠지 C# 컴파일러가 추상화한 부분을 걷어내고 싶어졌습니다. 혹시나 C# async/await 내부 동작을 이해하고 싶은 분들이 계시다면 이 글이 도움이 될 듯합니다. (또는, 수많은 소스코드 파일을 빌드하느라 힘에 겨운 C# 컴파일러의 일을 덜어주고 싶은, 착한 마음씨를 가진 개발자분들을 위해서도! ^^)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 우선 다음과 같은 예제 코드를 보겠습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { // C# 7.1 async Main static async Task Main(string[] args) { Program pg = new Program(); <span style='color: blue; font-weight: bold'>await pg.CallAsync();</span> } private async Task CallAsync() { string title = DateTime.Now.ToString(); <span style='color: blue; font-weight: bold'>string text = await GetFileContents(); Console.WriteLine(title + ": " + text);</span> } private async Task<string> GetFileContents() { <span style='color: blue; font-weight: bold'>return await new TaskFactory().StartNew(() => { return "test"; });</span> } } } </pre> <br /> 보는 바와 같이 async/await을 사용한 전형적인 비동기 함수 호출인데요. 실행하면 다음과 같은 식의 결과가 출력됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 2017-11-07 오후 9:05:15: test </pre> <br /> <a name='gen_src'></a> 자, 그럼 이 부분에서 GetFileContents 비동기 함수를 C# 컴파일러가 아닌 우리가 직접 비동기 처리로 바꿔보겠습니다. 방법은 사실 매우 쉽습니다. .NET Reflector와 같은 역어셈블러를 이용해 GetFileContents를 어떻게 바꿨는지 살펴보면 됩니다. 다음은 실제로 C# 컴파일러가 비동기 처리를 위해 만든 내부 클래스입니다.<br /> <br /> <pre style='height: 400px; margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > [CompilerGenerated] private sealed class <GetFileContents>d__2 : IAsyncStateMachine { // Fields public int <>1__state; public Program <>4__this; private string <>s__3; public AsyncTaskMethodBuilder<string> <>t__builder; private TaskAwaiter<string> <>u__1; private Task<string> <getStringTask>5__1; private string <urlContents>5__2; // Methods private void MoveNext() { string str; int num = this.<>1__state; try { TaskAwaiter<string> awaiter; if (num != 0) { this.<getStringTask>5__1 = new TaskFactory().StartNew<string>(Program.<>c.<>9__2_0 ?? (Program.<>c.<>9__2_0 = new Func<string>(Program.<>c.<>9.<GetFileContents>b__2_0))); awaiter = this.<getStringTask>5__1.GetAwaiter(); if (!awaiter.IsCompleted) { this.<>1__state = num = 0; this.<>u__1 = awaiter; Program.<GetFileContents>d__2 stateMachine = this; this.<>t__builder.AwaitUnsafeOnCompleted<TaskAwaiter<string>, Program.<GetFileContents>d__2>(ref awaiter, ref stateMachine); return; } } else { awaiter = this.<>u__1; this.<>u__1 = new TaskAwaiter<string>(); this.<>1__state = num = -1; } this.<>s__3 = awaiter.GetResult(); this.<urlContents>5__2 = this.<>s__3; this.<>s__3 = null; str = this.<urlContents>5__2; } catch (Exception exception) { this.<>1__state = -2; this.<>t__builder.SetException(exception); return; } this.<>1__state = -2; this.<>t__builder.SetResult(str); } [DebuggerHidden] private void SetStateMachine(IAsyncStateMachine stateMachine) { } } </pre> <br /> 위의 소스코드는 지저분하니, 다듬어서 재작성해보겠습니다.<br /> <br /> GetFileContents 메서드는 단 한 줄로 작성되어 있지만 다음과 같이 나눠볼 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > private async Task<string> GetFileContents() { Task<string> getStringTask = new TaskFactory().StartNew(() => { return "test"; }); string urlContents = await getStringTask; return urlContents; } </pre> <br /> C# 컴파일러는 위와 같은 async 메서드를 만나면 다음과 같이 2가지 단계로 분할합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > [Part A - 현재 스레드에서 실행할 코드] Task<string> getStringTask = new TaskFactory().StartNew(() => { return "test"; }); </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > [Part B - 별도의 스레드에서 실행할 코드] string urlContents = [getStringTask 작업의 반환값]; return urlContents; </pre> <br /> 그리고 저 코드들을 나눠 담을 IAsyncStateMachine 인터페이스를 상속한 별도의 내부 클래스를 정의합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > /* public interface IAsyncStateMachine { void MoveNext(); void SetStateMachine(IAsyncStateMachine stateMachine); } */ <span style='color: blue; font-weight: bold'>class GetFileContents_StateMachine : IAsyncStateMachine</span> { // ... [생략]... } </pre> <br /> GetFileContents_StateMachine 타입에는 내부 필드를 다음과 같은 식으로 포함하고 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // [async 동작을 위한 필드 3개] public int _state; public AsyncTaskMethodBuilder<string> _builder; TaskAwaiter<string> _awaiter; // [async 메서드를 구현하고 있는 클래스의 this 보관 필드] public Program _this; // [async 메서드의 반환값을 임시 보관하는 필드] string _result; // [async 메서드의 반환값을 보관하는 필드] string _urlContents; // [Part A 코드의 변수들] Task<string> _getStringTask; </pre> <br /> 이런 내부 필드 중에서 public 필드의 경우에는 C# 컴파일러가 async 메서드를 일반 메서드로 바꾸는 과정에서 다음과 같은 초기화를 해줍니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > private Task<string> GetFileContents() { GetFileContents_StateMachine stateMachine = new GetFileContents_StateMachine { <span style='color: blue; font-weight: bold'>_this = this, _builder = AsyncTaskMethodBuilder<string>.Create(), _state = -1,</span> }; stateMachine._builder.<span style='color: blue; font-weight: bold'>Start</span>(ref stateMachine); return stateMachine._builder.Task as Task<string>; } </pre> <br /> 보는 바와 같이 C# 컴파일러는 async 메서드를 일반 메서드로 바꾸면서 내부 동작을 GetFileContents_StateMachine 타입 내에 넣어두고는 stateMachine._builder.Start 메서드를 호출하는 걸로 마무리를 합니다.<br /> <br /> 여기서 _builder.Start는 비동기 호출이 아닙니다. 현재 스레드에서 시작하는 동기 호출에 불과합니다. AsyncTaskMethodBuilder 타입의 _builder 인스턴스는 Start 메서드 내에서 인자로 들어온 stateMachine의 MoveNext 메서드를 실행하는데, 이 때문에 Part A로 분리한 코드들은 이때 실행이 되도록 MoveNext가 구성되어 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > void IAsyncStateMachine.MoveNext() { string str; <span style='color: blue; font-weight: bold'>int num = this._state;</span> try { TaskAwaiter<string> awaiter; // 원래는 if 문이지만 명확한 분리를 위해 switch로 바꿨습니다. switch (<span style='color: blue; font-weight: bold'>num</span>) { case 0: // ...[생략]... break; default: <span style='color: blue; font-weight: bold'>this._getStringTask = new TaskFactory().StartNew(() => { return "test"; });</span> awaiter = this._getStringTask.GetAwaiter(); if (awaiter.IsCompleted == false) { <span style='color: blue; font-weight: bold'>this._state = num = 0;</span> this._awaiter = awaiter; GetFileContents_StateMachine stateMachine = this; this.<span style='color: blue; font-weight: bold'>_builder.AwaitUnsafeOnCompleted</span>(ref awaiter, ref stateMachine); return; } break; } // ...[생략]... } catch (Exception e) { // ...[생략]... return; } // ...[생략]... } </pre> <br /> C# 컴파일러가 GetFileContents 메서드를 async에서 일반 메서드로 변경하는 중에 _state 필드를 -1로 초기화했기 때문에 위의 MoveNext 메서드는 동기적으로 default 영역의 코드를 실행하게 됩니다.<br /> <br /> 보는 바와 같이 Part A로 분리되었던 영역의 코드가 default 영역에 추가되어 있고 await 코드의 대상이었던 _getStringTask에 대해 GetAwaiter()를 호출해 TaskAwaiter를 보관한 다음 해당 Task가 금방 끝나서 IsCompleted == true가 되면 더 이상 동작을 하지 않고 MoveNext를 반환합니다. 즉, 이런 경우에는 단순히 동기 메서드 호출한 것과 다를 바가 없습니다.<br /> <br /> 반면, 대개의 경우 awaiter.IsCompleted == false로 나오는데, 그럴 때는 다시 _builder.AwaitUnsafeOnCompleted 메서드를 호출해 작업이 완료된 경우의 알림을 등록합니다. 결국 stateMachine 객체의 MoveNext를 다시 호출하도록 만들고는 동기 호출을 마무리합니다. 그리고 이때의 _state 값은 0으로 설정했기 때문에 다음번 MoveNext가 호출될 때는 switch의 case 0 영역의 코드가 실행됩니다.<br /> <br /> 그래서 Task의 작업이 완료되었을 때 실행되는 MoveNext의 _state == 0으로 실행할 코드에는 C# 컴파일러가 분리한 Part B 영역의 코드를 포함하게 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > void IAsyncStateMachine.MoveNext() { string str; int num = this._state; try { TaskAwaiter<string> awaiter; switch (num) { <span style='color: blue; font-weight: bold'>case 0: // 작업 완료 후 실행되는 코드 awaiter = this._awaiter; this._awaiter = new TaskAwaiter<string>(); this._state = num = -1; break;</span> default: // ...[생략]... return; } <span style='color: blue; font-weight: bold'>this._result = awaiter.GetResult(); this._urlContents = this._result; // Part B의 코드</span> this._result = null; str = this._urlContents; } catch (Exception e) { this._state = -2; this._builder.SetException(e); return; } this._state = -2; this._builder.SetResult(str); } </pre> <br /> 이렇게 만들어두고 실행해 보면, C# 컴파일러가 제공했던 async/await 치환 코드와 정확히 동일한 결과를 얻게 됩니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 하는 김에 CallAsync 메서드도 바꿔 볼까요? ^^<br /> <br /> 이것도 메서드를 다음과 같이 2부분으로 나눌 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > [Part A - 현재 스레드에서 실행할 코드] string title = DateTime.Now.ToString(); [Task 객체 반환 = ]GetFileContents(); </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > [Part B - 별도의 스레드에서 실행할 코드] string text = [GetFileContents 메서드의 반환값]; Console.WriteLine(title + ": " + text); </pre> <br /> 마찬가지로 Part A, B 영역의 처리에 필요한 변수와 StateMachine 구현을 위한 필드를 가진 타입을 정의하고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > class CallAsync_StateMachine : IAsyncStateMachine { public int _state; public AsyncTaskMethodBuilder _builder; TaskAwaiter<string> _awaiter; public Program _this; string _result; // 반환값 string _text; // Part A 코드의 지역 변수 string _title; //...[생략]... } </pre> <br /> 마지막으로 Part A, B의 코드를 나눠서 실행할 MoveNext 메서드를 다음과 같이 구현해 주면 끝입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > void IAsyncStateMachine.MoveNext() { int num = this._state; try { TaskAwaiter<string> awaiter; switch (num) { case 0: // 작업 완료 후 Task 스레드에서 실행 awaiter = this._awaiter; this._awaiter = new TaskAwaiter<string>(); this._state = num = -1; break; default: // 동기적으로 실행될 코드 - Part A 코드를 포함 this._title = DateTime.Now.ToString(); <span style='color: blue; font-weight: bold'>awaiter = _this.GetFileContents().GetAwaiter();</span> if (awaiter.IsCompleted == false) { this._state = num = 0; this._awaiter = awaiter; CallAsync_StateMachine stateMachine = this; this._builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine); return; } break; } // 작업 완료 후 Task 스레드에서 실행 - Part B 코드를 포함 this._result = awaiter.GetResult(); this._text = this._result; this._result = null; <span style='color: blue; font-weight: bold'>Console.WriteLine(this._title + ": " + this._text);</span> } catch (Exception e) { this._state = -2; this._builder.SetException(e); return; } this._state = -2; this._builder.SetResult(); } </pre> <br /> 이것으로 완벽하게 C# 컴파일러가 대신 만들어 주었던 코드와 일치하므로 실행해 보면 정상적으로 다음과 같은 식의 결과를 볼 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 2017-11-07 오후 9:05:15: test </pre> <br /> 이렇게 풀어놓고 보니까... C# async/await 코드가 그다지 신기하지 않게 보입니다. ^^<br /> <br /> (<a target='tab' href='http://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1178&boardid=331301885'>첨부 파일은 이 글의 모든 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1309
(왼쪽의 숫자를 입력해야 합니다.)