Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a, b task switch #90

Open
zerozoo-a opened this issue Oct 10, 2023 · 3 comments
Open

a, b task switch #90

zerozoo-a opened this issue Oct 10, 2023 · 3 comments

Comments

@zerozoo-a
Copy link
Owner

function* a(b, count) {
  console.log('in a', count)
  if(count === 0) yield;
  while(count > 0) {
    yield* b(a, count-1);
  }
}

function* b(a, count) {
  console.log('in b', count)
  if(count === 0) yield;
  while(count > 0) {
    yield* a(b, count-1);
  }
}


function main() {
  const start = a(b, 6);
  console.log("🚀 ~ file: index.js:15 ~ main ~ start:", start.next())
}
main();
@zerozoo-a
Copy link
Owner Author

main thread와 iterator의 제어권 양도를 가능하게 하는 코드

function* gene(max,load,block) {
  let i = 0, curr = load;
  while(i < max) {
    if(curr--) {
      block(i);
      i++;
    } else {
      curr = load;
      console.log(i);
      yield;
    }
  }
}

function nbFor(...maxLoadBlock){
  const iterator = gene(...maxLoadBlock);
  function f(_) {
    iterator.next().done || setTimeout(f,0)
  }

  setTimeout(f, 0)
}


function main() {
  function hi(i){
    console.log('hi',i);
  }
  nbFor(100, 10, hi)
}
main();

@zerozoo-a
Copy link
Owner Author

기능은 같지만 비동기 stack에 올려 놓는 공간을 변경함,

1
2
3
4
5
6
in nextTick 1
in coroutine 1
in coroutine 2
in nextTick 2
in nextTick 3
in coroutine 3
in coroutine 4
in coroutine 5
in coroutine 6
in coroutine 7
in coroutine 8
in coroutine 9
in coroutine 10
in coroutine 11
in coroutine 12
in coroutine 13
in coroutine 14
in coroutine 15
in coroutine 16
in coroutine 17
in coroutine 18
in coroutine 19
in coroutine 20
in coroutine 21
in coroutine 22
in coroutine 23
in coroutine 24
in coroutine 25
in coroutine 26
in coroutine 27
in coroutine 28
in coroutine 29
in coroutine 30
in setTimeout 1
in setTimeout 3
in setTimeout 4
in setTimeout 5

출력을 보면 coroutine을 모방한 함수인 nbFor는 메인쓰레드에 안정적으로 자리를 내어주지만(정확히는 같은 쓰레드에서 돌지만
자신의 작업을 suspending 하고 기존 flow를 따라가다가 자신의 순서가 돌아오면 resume하는 것 단, 이렇게 생각하는 것이 편하고 다른 언어 kotline, java, go등을 사용할 때 편리할 듯)

무겁지 않은 작업의 경우엔 거의 연이어 실행 되는 것은 어쩔 수 없을 것이다. (시간내에 모두 실행되어지므로)

function* gene(max,load,block) {
  let i = 0, curr = load;
  while(i < max) {
    if(curr--) {
      block(i);
      i++;
      console.log('in coroutine',i);
    } else {
      curr = load;
      yield;
    }
  }
}

function nbFor(...maxLoadBlock){
  const iterator = gene(...maxLoadBlock);
  function f(_) {
    iterator.next().done || process.nextTick(f)
  }

  process.nextTick(f)
}


function main() {
  function hi(){
  }
  console.log('1')
  process.nextTick(()=>{console.log('in nextTick 1')})
  nbFor(30, 2, hi)
  console.log('2')
  console.log('3')
  console.log('4')
  setTimeout(()=>{
  console.log('in setTimeout 1')
  }, 0)
  console.log('5')
  setTimeout(()=>{
  console.log('in setTimeout 3')
  }, 0)
  process.nextTick(()=>{console.log('in nextTick 2')})
  console.log('6')
  setTimeout(()=>{
  console.log('in setTimeout 4')
  }, 0)
  process.nextTick(()=>{console.log('in nextTick 3')})
  setTimeout(()=>{
  console.log('in setTimeout 5')
  }, 0)
}
main();

@zerozoo-a
Copy link
Owner Author

내부에서 돌아가는 함수의 task를 무겁게하면 확실히 메인쓰레드에 자리를 내어주는 것이 유의미하다.

함수를 변형해서 실행 횟수와 실행 시간을 받아 메인쓰레드를 점유하고 있는 시간이 일정 시간 지나면 yield하도록 수정해봐야겠다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant