Multiprocessing vs. multi-threading with Python

Asked 1 years ago, Updated 1 years ago, 66 views

Which is better, multiprocessing or multi-threading? I heard that you don't need to use GIL for multiprocessing, is there anything else?

Please tell us the difference and pros and cons of the two

multithreading python multiprocessing

2022-09-22 15:11

1 Answers

Threading means writing multiple threads and multiprocessing means writing multiple processes

A process is larger than a thread because it can contain multiple threads within one process.

Threads use the same memory space within the process The process writes different memory spaces.

Therefore, it is difficult to share the same object between processes when using multiprocessing It's not always good to share objects, but when multiple threads use the same resource at the same time, race conditions can occur, resulting in unexpected results.

For example, if you want to append an element to a list one by one to make mylist = [1,2,3,...,300], Create multiple threads to speed up code like this.

import thread, time
mylist = [[0,1]]

def listTo300Elem(id):
    When there are more than 300 elements, it stops
        Mylist.append([id, mylist[-1][1]+1]) #Mark which thread is appended

thread.start_new_thread(listTo300Elem,(1,)) #threadid1
thread.start_new_thread(listTo300Elem,(2,)) #threadid2
thread.start_new_thread(listTo300Elem,(3,)) #threadid3
thread.start_new_thread(listTo300Elem,(4,)) #threadid4
thread.start_new_thread(listTo300Elem,(5,)) #threadid5
thread.start_new_thread(listTo300Elem,(6,)) #threadid6
thread.start_new_thread(listTo300Elem,(7,)) #threadid7

Time.sleep(5) #Wait long enough
print mylist

Result:

[[0, 1], [1, 2], [1, 3], [1, 4], [1, 5], ..., [7, 222], [7, 223], [3, 224], [3, 225], [3, 226], [3, 227], [3, 228], [3, 229], [3, 230], [3, 231], [3, 232], [3, 233], [3, 234], [3, 235], [3, 236], [3, 237], [3, 238], [3, 239], [3, 240], [3, 241], [3, 242], [3, 243], [3, 244], [3, 245], [3, 246], [3, 247], [3, 248], [6, 249], [6, 250], [6, 251], [6, 252], [6, 253], [6, 254], [6, 255], [6, 256], [6, 257], [6, 258], [6, 259], [6, 260], [6, 261], [6, 262], [6, 263], [6, 264], [6, 265], [6, 266], [6, 267], [6, 268], [6, 269], [6, 270], [6, 271], [6, 272], [6, 273], [6, 274], [6, 275], [6, 276], [6, 277], [6, 278], [6, 279], [6, 280], [6, 281], [6, 282], [6, 283], [6, 284], [6, 285], [6, 286], [6, 287], [6, 288], [6, 289], [6, 290], [6, 291], [6, 292], [6, 293], [6, 294], [6, 295], [6, 296], [6, 297], [6, 298], [3, 299], [3, 300], [7, 224], [6, 225], [1, 107]]

As expected, the last element should be [some id, 300], but 107 came out instead of 300. This code even produces different results each time it is executed.

The reason why each run results differently is that thread accesses the same resource at the same time. thread7 approaches mylist to output [7,223] and blocks it, and before outputting [7,224], other threads approach mylist to change the value of mylist. However, since thread7 does not know that 233 is no longer the last element, output [7, 224] and exit the function because myrist is more than 300.

This situation of using the same memory at the same time can cause problems To prevent this situation, a global interpreter lock (GIL) is needed to restrict access to resources.

In my opinion, the pros and cons of the two are


2022-09-22 15:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.