2012-11-27 2 views
0

http://effbot.org/zone/thread-synchronization.htm에 설명 된 것처럼 인터프리터에서 원자 연산을 사용하여 두 스레드간에 변수를 공유하고 싶습니다. 핵심 데이터 유형의 간단한 할당 (단일 바이트 코드 연산)은 스레드 안전성, 파이썬에서 GIL의 beacuse이어야합니다 < 3.2. 지금까지의 이론. 다음 코드는 마스터 또는 슬레이브 모드 (-m 또는 -s)에서 실행할 수 있습니다. 마스터 모드는 UDP를 통해 지속적으로 데이터를 전송합니다. 슬레이브 모드는 udp 포트에서 데이터를 읽고 수신 된 각 패킷의 변수를 업데이트하는 스레드를 작성합니다.파이썬에서 두 스레드간에 변수 공유하기

예제 코드는 작성시 공유 변수를 스레드로 전달합니다. 전역 변수를 사용하거나 스레드 로컬 저장소를 스레드에 전달하여 시도했습니다.

결과도 동일합니다. read_time_master 스레드 내부에서 변수가 할당됩니다. 그러나 주 스레드에서 공유 변수의 값은 업데이트되지 않습니다.

#!/usr/bin/env python 

import socket 
import itertools 
import multiprocessing 
from optparse import OptionParser 
from time import sleep 

PORT = 1666 

def read_time_master(sock, time_master): 
    while True: 
    time_master = float(sock.recvfrom(1024)[0]) 

def main(): 
    time_master = 0.0 
    p = OptionParser() 
    p.add_option('--master', '-m', action='store_true') 
    p.add_option('--slave', '-s', action='store_true') 
    options, arguments = p.parse_args() 
    if options.master or options.slave: 
     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) 
    if options.master: 
     sock.connect(('127.0.0.1', PORT)) 
    if options.slave: 
     sock.bind(('0.0.0.0', PORT)) 
     recv_thread = multiprocessing.Process(target=read_time_master, args=(sock, time_master)) 
     recv_thread.start() 

    for time in itertools.count(): 
     print time 
     if options.slave: 
      print "master: %f" % time_master # -> not updated from other thread 
     if options.master: 
      try: 
       sock.send(str(time)) 
      except socket.error: 
       pass 
     sleep(1) 

    if options.master or options.slave: 
     sock.close() 

if __name__ == '__main__': 
    main() 
+1

스레드가 아닌 프로세스 ('다중 처리 ')를 사용 중이므로 프로세스간에 변수를 공유 할 수 없습니다 (여분의 기계가 많이 필요함). – Gerrat

답변

5

당신은 당신의 상황에 도움이되지 multiprocessing하지 threading를 사용하고 있습니다. 배경 작업자로 threading.Thread을 사용했다면 일 것입니다.은 백그라운드 작업에 의해 제어되는 기능 내에서 global time_master 전화를 걸기 만하면 필요한 것을 얻을 수 있습니다. threading이 아닌 multiprocessing을 사용 중이므로 프로세스간에 정보를주고 받거나 동기화하는 데 사용할 수있는 컨테이너의 경우 multiprocessing.Queue 클래스를 조사해야합니다. 당신은 또한 (이 모두가 multiprocessing 문서에 덮여뿐만 아니라 프로세스간에 공유되는 변수를 만들 수 있습니다 /의 예제 Python Homepage

관련 문제