简体   繁体   English

如何在python上同时运行两个线程?

[英]How to run two threads at the same time at python?

I already read this topic, but when I try to run this code, I will a little delta 我已经阅读了主题,但是当我尝试运行此代码时,我会稍微

import threading
from threading import Thread
from cryptography.fernet import Fernet
import time
from multiprocessing import Process



def create_key1():

    print(time.time())


def create_key2(): 

    print(time.time())  

if __name__ == '__main__':
    Process(target = create_key1()).start()
    Process(target = create_key2()).start()

    Thread(target = create_key1()).start()
    Thread(target = create_key2()).start()

if we comment Process and run the code, we will see the result : 如果我们注释Process并运行代码,我们将看到结果:

1501843580.508508
1501843580.5089302

if we comment Thread and run the code, we will see the result : 如果我们注释线程并运行代码,我们将看到结果:

1501843680.4178944
1501843680.420028

we got delta at the same situation, my question is how to run threads at the same time, be cause I want check generation of the key in cryptography python library. 我们在相同情况下遇到了增量问题,我的问题是如何同时运行线程,因为我想检查密码python库中密钥的生成。 I want to check what will if I try to generate two keys at same time, will they same or not. 我想检查一下,如果我尝试同时生成两个密钥,它们是否相同,该怎么办?

Parallel processing of two functions, as in your code, does not guarantee that the functions will run at exactly the same time. 与您的代码中一样,对两个函数的并行处理不能保证这些函数将在完全相同的时间运行。 As you have seen there is a slight discrepancy in the time that the methods reach the time.time() call, and this is to be expected. 如您所见,方法到达time.time()调用的时间略有差异,这是可以预期的。

In particular due to the way that the threading module is designed it isn't possible for the methods to run at exactly the same time. 特别是由于threading模块的设计方式,方法不可能完全同时运行。 Similarly, while the multiprocessing module could theoretically run two functions at the exact same time there is no guarantee of this, and it is likely to be a rare occurrence. 类似地,尽管理论上multiprocessing模块可以同时精确地运行两个功能,但不能保证这一点,而且这种情况很少发生。

In the end this is butting up against the low level constraints of an operating system, where two pieces of code can't physically be run at the same time on the same processing core. 最后,这与操作系统的低级限制相抵触,在操作系统中,无法在同一处理内核上同时运行两个代码。

To answer your question on how this will affect the keys produced by your code, it depends on how sensitive your algorithm to the current time. 要回答有关这将如何影响代码产生的键的问题,这取决于算法对当前时间的敏感程度。 If your algorithm bases a key of the current time to the nearest second, or tenth of a second then the keys produced will likely be identical (but are not guaranteed to be). 如果您的算法将当前时间的密钥基于最近的秒或十分之一秒,则生成的密钥可能会相同(但不能保证是相同的)。 However if the keys produced are based on the exact time that the function call is reached then they are unlikely to ever match, as there is no guarantee of the time the function calls will be reached in the two functions. 但是,如果生成的键基于到达函数调用的确切时间,则它们不可能匹配,因为不能保证两个函数中到达函数调用的时间。

For more information on the differences between the threading and multiprocessing modules see this . 有关threadingmultiprocessing模块之间差异的更多信息,请参见this

The GIL is an interpreter-level lock. GIL是解释程序级别的锁。 This lock prevents the execution of multiple threads at once in the Python interpreter. 此锁可防止在Python解释器中一次执行多个线程。 Each thread that wants to run must wait for the GIL to be released by the other thread, which means your multi-threaded Python application is essentially single threaded, 每个要运行的线程都必须等待另一个线程释放GIL,这意味着您的多线程Python应用程序本质上是单线程的,

Another approach is to use the multiprocessing module where each process runs in its own OS process with its own Python runtime. 另一种方法是使用多处理模块,其中每个进程都使用自己的Python运行时在其自己的OS进程中运行。 You can take full advantage of multiple cores with this approach, and it's usually safer because you don't have to worry about synchronising access to shared memory. 通过这种方法,您可以充分利用多个内核,并且通常更安全,因为您不必担心同步访问共享内存。

for more info about [GIL] 1 有关[GIL] 1的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM