2014-05-22 1 views
2

이 테스트는 python2.6/2.7에서 성공하지만 pypy에서는 실패합니다. pypy는 확실히 스레드를 지원하며 imp 잠금은 pypy에서 예상대로 스레드없이 작동하는 것 같습니다. 스레드가 자신의 수입 잠금을 받고 것 같다? ....pypy에서 가져 오기 잠금이 작동하지 않음

import unittest 
import imp 
import threading 


class Test(unittest.TestCase): 

    def test_import_lock(self): 
     lock_held = threading.Event() 
     test_complete = threading.Event() 
     lock_released = threading.Event() 

     def other_thread(): 
      imp.acquire_lock() 

      assert imp.lock_held() # <--- this assertion passes, the imp lock is definitely held 

      lock_held.set() 
      test_complete.wait() 
      imp.release_lock() 

      lock_released.set() 

     t = threading.Thread(target=other_thread) 
     t.setDaemon(True) 
     t.start() 

     # Wait until the thread is holding the import lock 
     lock_held.wait() 

     # The import lock should be held at this point. The thread even asserted 
     # that it was held! 
     lock_was_held = imp.lock_held() 

     # Notify the thread to release the lock 
     test_complete.set() 

     # Wait for the lock to be released 
     lock_released.wait() 

     # Make sure the lock was held when we expected it to be held 
     assert lock_was_held 

if __name__ == '__main__': 
    unittest.main() 

pypy

struys$ python --version 
Python 2.7.6 (394146e9bb67, May 08 2014, 16:45:59) 
[PyPy 2.3.0 with GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] 
struys$ python -m test 
F 
====================================================================== 
FAIL: test_import_lock (__main__.Test) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "test.py", line 42, in test_import_lock 
    assert lock_was_held 
AssertionError 

---------------------------------------------------------------------- 
Ran 1 test in 0.001s 

FAILED (failures=1) 

2.7

struys$ python --version 
Python 2.7.2 
struys$ python test.py 
. 
---------------------------------------------------------------------- 
Ran 1 test in 0.000s 

OK 
+0

당신은라는 변수 lock_held이 imp.lock_held라고하는 imp에 관한 메소드입니다. 이는 독자를 혼동시킬 수 있지만 문제의 원인은 아닙니다. – Jim

답변

3

는 PyPy의 구현을 반환 여부 같은데 파이썬 자물쇠는이 스레드에 의해 개최됩니다 : https://bitbucket.org/pypy/pypy/src/6e9376d22e0ecc83bfcdda81d0e37e695b435dd7/pypy/module/imp/importing.py?at=default#cl-753

잠금이 모든에서 개최 여부를 CPython과의 수익률 반면 : http://hg.python.org/cpython/file/3a1db0d2747e/Python/import.c#l348

그래서 내가 발권 한 PyPy의 정품 버그로 보이는 : https://bitbucket.org/pypy/pypy/issue/1775/implock_held-isnt-quite-right

+2

이 버그는'default'에서 수정되었습니다. –

관련 문제