python,为什么打印在线程处理期间不跳换行符

时间:2019-12-07 14:05:35

标签: 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 - 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)
如上所示,

如何将两种不同的打印合并为一行。谢谢。

0 个答案:

没有答案