루비 애플리케이션에서 나는 아무 상태도 공유하지 않는 작업들을 많이 가지고 있으며 한 번에 여러 개를 시작하려고합니다. 결정적으로, 나는 시작한 순서 나 반환 값에 신경 쓰지 않는다. (완료되기 전에 각각 데이터베이스 트랜잭션이 발생하기 때문에). 내 루비 구현에 따라 GIL이 실제로 동시에 실행되는 작업을 막을 수는 있지만 실제로는 동시성에 관심이 없기 때문에 괜찮습니다. 이러한 작업자 스레드는 네트워크 요청에 대해 IO 바인딩이됩니다. 내가 지금까지있어 무엇루비의 IO 바운드 스레드
는 this입니다 :
def asyncDispatcher(numConcurrent, stateQueue, &workerBlock)
workerThreads = []
while not stateQueue.empty?
while workerThreads.length < numConcurrent
nextState = stateQueue.pop
nextWorker =
Thread.new(nextState) do |st|
workerBlock.call(st)
end
workerThreads.push(nextWorker)
end # inner while
workerThreads.delete_if{|th| not th.alive?} # clean up dead threads
end # outer while
workerThreads.each{|th| th.join} # join any remaining workers
end # asyncDispatcher
그리고 나는 이런 식으로 호출 :
asyncDispatcher(2, (1..10).to_a) {|x| x + 1}
에 여기 숨어 버그 나 동시성 함정이 있습니까? 아니면이 작업을 단순화하는 런타임의 무언가입니까?
celluloid.io를보십시오. 주 루프가 너무 바빠서 기다리는 것처럼 보입니다. –
++는 바쁜 대기 중입니다. : 'workerThreads.length
7stud
전에 작업자 스레드가 자고 작업 스레드가 완료되면 잠에서 깨어나지만이 접근법은 많은 코너 케이스를 가져야하고 더 많은 복잡한 잠금이 필요했습니다. –