【问题标题】:Python Thread() deleting the newline?Python Thread() 删除换行符?
【发布时间】:2020-12-05 10:13:42
【问题描述】:

我在我的程序中遇到了一个奇怪的错误。代码背后的一般想法是,首先创建一个包含 5 个列表的数组(称为 lists_of_nums)。每个列表包含 100 个 1 到 10 000 范围内的数字。然后程序运行 5 个线程。每个人都找到一个列表的平均值,并打印出完成它所花费的时间。

您会注意到在 threading_function() 中 print() 语句的末尾有一个换行符。就在它说“秒”的地方。\n。

问题是,如果我将 't.join()' 放在单独的 'for' 循环中(我的代码的最底部),以便同时运行线程,则有时会删除换行符(显然)。如果我一个接一个地单独运行线程,它只能在 100% 的时间内工作。

我希望有人可以帮助我了解如何同时运行线程并且仍然让换行符可靠。

让我通过一个代码示例及其错误输出(仅在某些时候发生)来说明我的意思:

lists_of_nums = []

def create_set():    # Function for creating a list of random integers and appending them to the lists_of_nums array.

    random_nums = []

    for _ in range(100):

        random_nums.append(randint(1, 10000))

    lists_of_nums.append(random_nums)

for _ in range(5):    # Five of these lists get appended to the array, by using the create_set() function.
    create_set()


def threading_function(list_index):    # Function responsible for finding the mean value of a given list as well as the time taken to do it.

    start = timeit.default_timer()

    mean_value = mean(lists_of_nums[list_index])

    end = timeit.default_timer() - start

    print(
        "The mean value of the list number " + str(list_index + 1) + " is " + str(mean_value) +
        "\nThe time taken to find it was " + str(end) + " seconds.\n" # The abovementioned newline.
        )

threads = []

for i in range(len(lists_of_nums)):

    t = Thread(target = threading_function, args = [i])
    t.start()
    threads.append(t)

for t in threads:    # If t.join() remains in a separate 'for' loop than the Thread() class, the newline occasionally disappears.
    t.join()

输出不正确,换行符在打印语句 3 和 4 之间似乎消失了:

The mean value of the list number 1 is 5270.34
The time taken to find it was 0.00012170000000000236 seconds.

The mean value of the list number 2 is 4768.17
The time taken to find it was 9.239999999999943e-05 seconds.

The mean value of the list number 3 is 4766.67
The time taken to find it was 8.369999999999905e-05 seconds.
The mean value of the list number 4 is 4969.7
The time taken to find it was 9.880000000000305e-05 seconds.


The mean value of the list number 5 is 4686.21
The time taken to find it was 9.25000000000023e-05 seconds.

【问题讨论】:

    标签: python python-3.x multithreading newline


    【解决方案1】:

    正在打印换行符。请注意,语句 4 和 5 之间有额外的一行。您的线程之间可能存在竞争条件。尝试用锁保护print 函数。即,

    printLock = threading.Lock() # global variable
    

    然后

    # inside threading_function
    with printLock:
        print(
            "The mean value of the list number " + str(list_index + 1) + " is " + str(mean_value) +
            "\nThe time taken to find it was " + str(end) + " seconds.\n" # The abovementioned newline.
            )
    

    【讨论】:

    • 啊,非常感谢!我什至不知道数据竞赛的概念。我的代码现在按预期工作:)
    【解决方案2】:

    字符串中的换行符是完整的。 print 默认情况下会在之后添加一个换行符。它在单独的写入中执行此操作。来自不同线程的两次写入(原始字符串和换行符)可以交错,因此您有时还会看到双空行。如果您从字符串中删除换行符,您会看到类似的奇怪效果。

    一种解决方案是以两个换行符结束您的字符串,并阻止 print 添加自己的,即print("....\n\n", end="")

    【讨论】:

    • 我很确定将换行符融合到字符串中实际上并不是一种安全的方法,即使假设当前的 CPython 实现也是如此。中断的可能性可能较小,但我很确定它们仍有可能发生。
    猜你喜欢
    • 1970-01-01
    • 2015-09-09
    • 2011-07-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多