종료하는 동안 루비가 잠그게 할 수있는 이유는 무엇입니까?

StackOverflow https://stackoverflow.com/questions/2436754

  •  19-09-2019
  •  | 
  •  

문제

스크립팅 API를 통해 몇 가지 Perforce 작업을 수행하는 Ruby 스크립트가 있습니다.

def foo()
  ...
end

def bar()
  ...
end

foo()
bar()
puts __LINE__
exit 0
#end of file

... 그리고 그 동안 출구 (0)가 있는지 여부에 관계없이 프로세스가 끝나지 않습니다. 이것은 Ruby 1.8.6, 주로 Mac에서 이루어 지지만 PC에서도 이것을보고 있습니다.

나는 일반적인 Google을 찌르고 있지만 여기에 은행에 대한 경험의 목소리가 있기를 바랐습니다. 감사.

도움이 되었습니까?

해결책

나는 Perforce에 전혀 익숙하지 않지만 범인은 at_exit 어떤 이유로 든 루프에 갇힌 방법. 사용하는 동작을 관찰하십시오 irb, 예를 들어:

$ irb
irb(main):001:0> require 'time'
=> true
irb(main):002:0> at_exit { puts Time.now; sleep 10; puts Time.now }
=> #<Proc:0xb7656f64@(irb):2>
irb(main):003:0> exit
Fri Mar 12 19:46:25 -0500 2010
Fri Mar 12 19:46:35 -0500 2010

AN을 확인합니다 at_exit 기능은 프로세스가 매달려있게되며, at_exit. 주목하십시오 #{caller} 누가 전화 한 스택 추적을 표시합니다 at_exit:

def at_exit &block
    @at_exit_counter ||= 0
    puts "at_exit #{@at_exit_counter} called"
    s = "Calling at_exit ##{@at_exit_counter} from #{caller}"
    super { puts s; block.call() }
    @at_exit_counter += 1
end

at_exit { puts "I'll never return"; sleep 1 while true; }
at_exit { puts 'I am about to leave.' }

def do_cleanup
    puts "Cleaning..."
    at_exit { puts 'Cleaning up before exit...' }

end

do_cleanup

출력 :

at_exit 0 called
at_exit 1 called
Cleaning...
at_exit 2 called
Calling at_exit #2 from exitcheck.rb:14:in `do_cleanup'exitcheck.rb:18
Cleaning up before exit...
Calling at_exit #1 from exitcheck.rb:10
I am about to leave.
Calling at_exit #0 from exitcheck.rb:9
I'll never return

그리고 결코 돌아 오지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top