2017-11-14 29 views
0

this question의 후속 조치로, json 파일을 읽으려면 threadpoolexecutor을 시작하는 간단한 스크립트가 있습니다. 루프를 사용하여 1에서 9까지 카운트하고 싶다면 for 루프를 사용하십시오. 어떤 이유로 비록 내가 executor.shutdown(wait=False)을 사용했지만 여전히 블록을 수행하고 read_employees 메쏘드가 실행되기를 기다린다.블로킹없이 ThreadPoolExecutor 사용

에 따르면 documentation :

대기는,이 메소드는 즉시 반환하고 대기중인 모든 선물이

import concurrent.futures 
import json 
import time 


def read_employees(read_file): 
    with open(read_file) as f_obj: 
     employees = json.load(f_obj) 

    for emp in employees: 
     print(emp) 
     time.sleep(3) 


def start_thread(): 
    filename = 'employee.json' 
    with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: 
     executor.submit(read_employees, filename) 
     executor.shutdown(wait=False) 

def print_number(): 
    for num in range(1,10): 
     time.sleep(2) 
     print(num) 


start_thread() 
print_number() 
실행 완료되면 집행자와 관련된 자원이 해제됩니다 False이면

내가 이렇게하면 :

def read_employees(read_file): 
    with open(read_file) as f_obj: 
     employees = json.load(f_obj) 

    for emp in employees: 
     time.sleep(5) 
     print(emp) 


def print_number(): 
    for num in range(1,10): 
     print(num) 


filename = 'employee.json' 
empThread = threading.Thread(target=read_employees, args=(filename,)) 
empThread.start() 

print_number() 

먼저 1에서 9까지 계산 한 다음 직원을 인쇄합니다. 지연은 직원을 읽는 동안 수면으로 인한 것입니다. 그래서 같이 :

1 
2 
3 
4 
5 
6 
7 
8 
9 
[email protected] 
[email protected] 

어떻게 차단하지 않고 threadpoolexecutor를 사용하여 동일한 출력을 얻을 수 있습니까?

답변

0

이 작은 조각에 아마 인해 :

는 "당신은 당신이 with 문을 사용하는 경우 명시 적으로이 메소드를 호출하는 것을 방지 할 수있는 것이다 셧다운 Executor (대기 True-wait 세트라고했다 Executor.shutdown() 것처럼) "

https://docs.python.org/3/library/concurrent.futures.html

+0

이것은이지만 막고 것처럼 여전히 출력 :

두 번째 옵션은 같은 것을 할 것입니다. –

1

나는 당신이 with 문을 사용하지 않는 것이 좋습니다 것입니다. with 문은 context manager__exit__ 메서드를 호출하여 문을 닫습니다. 컨텍스트 매니저는 __enter____exit__ 메소드를 구현하는 모든 클래스입니다. 따라서 모든 것이 with 문 안에서 실행 된 후에 전달 된 컨텍스트 관리자에서 __exit__을 호출합니다.

이 경우 ThreadPoolExecutor은 컨텍스트 관리자입니다. ThreadPoolExecutorExecutor의 하위 클래스입니다. 따라서 Executor's class definition을 참조하면 __exit__ 메서드에서 self.shutdown(wait=True) 메서드를 호출하는 것을 볼 수 있습니다.

self.shutdown(wait=True)에 대한 호출이 문제입니다. 컨텍스트 관리자의 작동 방식을 따르는 경우 self.shutdown(wait=False)with 문에서 마지막 내용이므로 __exit__이 바로 호출됩니다. 그러면 self.shutdown(wait=True)이 호출됩니다. 그래서 그것이 당신을 방해합니다.

두 가지 방법으로 문제를 해결할 수 있습니다. 첫 번째는 ThreadPoolExecutor을 서브 클래스 화하고 __exit__ 메소드를 다시 작성하는 것입니다. 내가 FALSE '`에`wait`을 설정하는 이유

def start_thread(): 
    filename = 'employee.json' 
    executor = concurrent.futures.ThreadPoolExecutor(max_workers=2) 
    executor.submit(read_employees, filename) 
    executor.shutdown(wait=False)