Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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,为什么要打印don';在线程过程中不能跳转换行_Python_Multithreading_Printing_Newline - Fatal编程技术网

python,为什么要打印don';在线程过程中不能跳转换行

python,为什么要打印don';在线程过程中不能跳转换行,python,multithreading,printing,newline,Python,Multithreading,Printing,Newline,我的问题很简单:为什么一些印刷品不能进入下一个新行 以下是我的代码: def inner_func(lock): with lock: print (f'{threading.current_thread().getName()} - inner_function - lock1 - {time.time()}') print (f'{threading.current_thread().getName()} - inner_function - lock

我的问题很简单:为什么一些印刷品不能进入下一个新行

以下是我的代码:

def inner_func(lock):
    with lock:
        print (f'{threading.current_thread().getName()} - inner_function - lock1 - {time.time()}')
        print (f'{threading.current_thread().getName()} - inner_function - lock2 - {time.time()}')
        print (f'{threading.current_thread().getName()} - inner_function - lock3 - {time.time()}')
        time.sleep(1)
    print(f'{threading.current_thread().getName()} - inner_function - {time.time()}')

def outer_func(a, lock):
    inner_func(lock)
    print(f'{threading.current_thread().getName()} - outsider_function - input: {a} - {time.time()}')

class Worker():
    def __init__(self, num_threads, input_item):
        self.t             = threading
        self.lock          = self.t.Lock()
        self.q             = queue.Queue()
        self.num_thread    = num_threads
        self.input_item    = input_item

    def worker(self):
        while True:
            item = self.q.get()
            if item:    item.append(self.lock)
            if not item:
                break
            # outer_func(*item)
            outer_func(*item)
            self.q.task_done()

    def main(self):
        threads = []
        for _ in range(self.num_thread):
            t = self.t.Thread(target=self.worker)
            t.start()
            threads.append(t)

        for item in self.input_item:
            self.q.put(item)
        self.q.join()
        for _ in range(self.num_thread):
            self.q.put(None)
        for t in threads:
            t.join()

container = [['a'], ['b'], ['c'], ['d'], ['e'], ['f'], ['g']]
Worker(7, container).main()
以下是我的一些打印输出:

.....(some other print)
Thread-5 - inner_function - 1575727105.8761373Thread-7 - inner_function - lock1 - 1575727105.8761373
.....(some other print)

正如您在上面所看到的,两种不同的打印合并成一行,如何防止它。谢谢

这回答了你的问题吗?好的,我明白了。简单的解决方案。感谢