简体   繁体   中英

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 :

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. 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.

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. 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.

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 .

The GIL is an interpreter-level lock. This lock prevents the execution of multiple threads at once in the Python interpreter. 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,

Another approach is to use the multiprocessing module where each process runs in its own OS process with its own Python runtime. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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