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.
Edit: ShadowRanger made a good comment about some context I missed when I originally answered this question, I've copied the comment below for best clarity:
This answer is correct in the general, but wrong in the specifics. The OP wasn't even running their code in processes or threads; they ran it in the main thread in the main process, then launched no-op threads/processes (because it used the return value of the functions as the target, and it was None
). To make it actually do anything outside the main thread, you'd need to use target=create_key1
(which passes the uncalled function in to be run in the thread/process), not target=create_key1()
(which runs the function and passes its return value in as the target).