Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从线程的每个循环获取返回值_Python_Multithreading - Fatal编程技术网

Python 从线程的每个循环获取返回值

Python 从线程的每个循环获取返回值,python,multithreading,Python,Multithreading,我有一个内部带有for循环的线程,我想从每个循环返回值。但是当我把return放在循环中时,它会永久地中断循环,所以我不能得到其他值 以下是我的功能: def track_duration_count(track_length): time.sleep(13.2) for i in range(0,track_length): timer = str(datetime.timedelta(seconds=i)) #sys.stdout.write(

我有一个内部带有for循环的线程,我想从每个循环返回值。但是当我把return放在循环中时,它会永久地中断循环,所以我不能得到其他值

以下是我的功能:

def track_duration_count(track_length):
    time.sleep(13.2)
    for i in range(0,track_length):
        timer = str(datetime.timedelta(seconds=i))
        #sys.stdout.write(str(i)+' ')
        #sys.stdout.flush()
        return timer
        print (timer)
        time.sleep(1)
        global stop_thread
        if stop_thread:
             break
    print ("BREAKFREE")
然后,我使用以下命令调用该函数:

_thread.start_new_thread(track_duration_count,(track_length,))

我想将此func中的
计时器
用于另一个线程。

我建议使用队列在线程之间共享数据。将队列传递给函数(它将传递引用而不是队列的副本,Python就是这样工作的),然后使用queue.put()方法在队列中放置计时器,而不是返回。然后,其他线程可以使用queue.get()方法检索该值


还有一个原因是你没有使用标准的threading.Thread类吗?除非你有很好的理由使用私有方法,否则使用这个方法会更安全。

你的问题不清楚……所以请回答并提供一个-这可能会澄清你试图做什么。1)使用一个继承自
threading.Thread的类,并将结果存储为属性2)使用
multiprocessing.dummy.Pool
s映射函数3)将结果添加到列表中,因为列表将作为引用传递。