将对象传递给两个线程

时间:2019-05-31 08:41:53

标签: python multithreading

我有3个线程。 1个线程收集数据并返回 var1 = Thread1.start()

线程2和线程3使用此变量var1进行例程。 我不确定是否做对了。因为有时返回var1并且它不是一个空列表,所以我将其存储在每个线程的变量中,使用列表推导来提取数据。在调试日志中,我看到了必须在线程3中的元素,但是该线程的调试记录器未返回任何内容。

在线程2和线程3算法中:

def __init__(self):
    self.lock = threading.RLock()

def do_smth2(self,var1):
   self.lock.acquire()
      var1_2 = var1
   self.lock.release()

def do_smth3(self,var3)
   self.lock.acquire()
     var1_3 = var1
   self.lock.release()

main

   object = thread1.start()
   thread2.start(object)
   thread3.start(object)

线程2和线程3同时运行,如果time.sleep(3)var1_3var1_2(这是None类型,则我使用list )或len(var1_3) <0

编辑

class Application:

    def __init__(self):
        self.logger = RootLogger()
        self.logger.set_config(__name__, sys_log)
        self.adapter = Adapter()
        self.transit_listener = TransitListener()

    def run(self):

        #start listeners
        transits_list = self.transit_listener.start()        
        self.adapter.start(transits_list)


        # start REST service
        RestWebService().run()

线程1

class TransitListener:
    def __init__(self):
         self.interval = session_interval

    def _transits_data(self):
        # while polling is running change interval after 1st cycle
        while datetime.now() >= session_interval:
            result = self.connector.query(self.statement,
                                          fetch=True)
            self.logger.debug(result)
            # store result
            self.transits_queue.put(result)
            self.logger.debug(self.interval)
            time.sleep(5)
            # change interval
            self._interval_granularity()
            self.logger.debug(self.interval)

     def start(self):
        self.worker = Thread(target=self._transits_data)
        self._configure()
        self.logger.info("Starting thread 'transists listener'...")
        try:
            self.worker.start()
            if self.worker.is_alive():
                self.logger.info("Thread 'transits listener' started")
                # return result from queue
                return self.transits_queue.get()

线程2和3

class Adapter:
    def __init__(self):
        self.logger = RootLogger()
        self.logger.set_config(name=__name__, logfile=epp_log)
        self.lock = RLock()
        self.threads = []


    def _session_start(self, transits):
        while datetime.now() >= session_interval:
            self.lock.acquire()
            transit_list = transits
            self.lock.release()
            self.logger.debug(f"ENTRIES {transit_list}")

    def _session_stop(self, transits):
        while datetime.now() >= session_interval:
            self.lock.acquire()
            transit_list = transits
            self.lock.release()
            self.logger.debug(f"EXITS{transit_list}")
  def start(self, transits):
        # prepare SQL tables
        # define priority of threads

        # 1st
        session_start_thread = Thread(target=self._session_start, args=(transits,))
        self.threads.append(session_start_thread)

        # 2nd
        session_stop_thread = Thread(target=self._session_stop, args=(transits,))
        self.threads.append(session_stop_thread)
        self.threads[0].start()
        self.threads[1].start()

0 个答案:

没有答案