Python'da Eşzamanlılık

Eşzamanlılık genellikle paralellik olarak yanlış anlaşılır. Eşzamanlılık, bağımsız kodun sistematik bir şekilde yürütülebilmesi için zamanlamayı ifade eder. Bu bölüm, Python kullanan bir işletim sistemi için eşzamanlılığın yürütülmesine odaklanmaktadır.

Aşağıdaki program, bir işletim sistemi için eşzamanlılığın yürütülmesine yardımcı olur -

import os
import time
import threading
import multiprocessing

NUM_WORKERS = 4

def only_sleep():
   print("PID: %s, Process Name: %s, Thread Name: %s" % (
      os.getpid(),
      multiprocessing.current_process().name,
      threading.current_thread().name)
   )
   time.sleep(1)

def crunch_numbers():
   print("PID: %s, Process Name: %s, Thread Name: %s" % (
      os.getpid(),
      multiprocessing.current_process().name,
      threading.current_thread().name)
   )
   x = 0
   while x < 10000000:
      x += 1
for _ in range(NUM_WORKERS):
   only_sleep()
end_time = time.time()
print("Serial time=", end_time - start_time)

# Run tasks using threads
start_time = time.time()
threads = [threading.Thread(target=only_sleep) for _ in range(NUM_WORKERS)]
[thread.start() for thread in threads]
[thread.join() for thread in threads]
end_time = time.time()

print("Threads time=", end_time - start_time)

# Run tasks using processes
start_time = time.time()
processes = [multiprocessing.Process(target=only_sleep()) for _ in range(NUM_WORKERS)]
[process.start() for process in processes]
[process.join() for process in processes]
end_time = time.time()

print("Parallel time=", end_time - start_time)

Çıktı

Yukarıdaki program aşağıdaki çıktıyı üretir -

Açıklama

"Çoklu işlem", diş açma modülüne benzer bir pakettir. Bu paket, yerel ve uzak eşzamanlılığı destekler. Bu modül sayesinde, programcılar verilen sistemde birden çok işlemi kullanma avantajına sahip olurlar.