An warware: taswirar sarrafawa da yawa

Multiprocessing sanannen fasaha ce a cikin shirye-shiryen Python wanda ke ba ku damar gudanar da matakai da yawa a lokaci guda, galibi yana haifar da haษ“aka aiki da ingantaccen amfani da albarkatun tsarin. Wannan labarin yana nutsewa cikin amfani da multiprocessing ษ—akin karatu a Python, musamman yana mai da hankali kan map aiki. Aikin taswirar yana ba ku damar amfani da aiki ga kowane abu a cikin mai iya aiki, kamar jeri, da mayar da sabon jeri tare da sakamako. Ta hanyar yin amfani da multiprocessing, za mu iya daidaita wannan tsari don ingantaccen aiki da haษ“aka.

A cikin wannan labarin, za mu bincika matsalar da multiprocessing tare da aikin taswira zai iya zama mafita mai kyau, tattauna ษ—akunan karatu da ayyuka masu dacewa, samar da bayanin mataki-mataki na lambar, da kuma shiga cikin batutuwa masu dangantaka da ke ginawa a kan kashin baya. na multiprocessing da aikin taswira.

Taswirar sarrafawa da yawa: Matsala da Magani

Matsalar da muke nufin warwarewa ita ce haษ“aka aiki da inganci na amfani da aiki ga kowane abu a cikin babban mai iya sarrafa shi, kamar jeri, tuple, ko wani abu mai goyan bayan maimaitawa. Lokacin fuskantar irin waษ—annan ayyuka, yin amfani da ginanniyar aikin taswira ko fahimtar lissafin na iya zama a hankali da rashin inganci.

Maganin shine a yi amfani da ษ—akin karatu da yawa a cikin Python, musamman, da pool class da ta map hanya. Ta hanyar amfani da Multiprocessing Pool.map() aiki, za mu iya rarraba aiwatar da aikin mu a cikin matakai masu yawa.

Bayanin mataki-mataki na Code

Bari mu rushe lambar kuma mu kwatanta yadda ake amfani da aikin taswira da yawa yadda ya kamata:

import multiprocessing
import time

def square(n):
    time.sleep(0.5)
    return n * n

# Create the list of numbers
numbers = list(range(10))

# Initialize the multiprocessing Pool
pool = multiprocessing.Pool()

# Use the map function with multiple processes
squared_numbers = pool.map(square, numbers)

print(squared_numbers)
  1. Na farko, shigo da multiprocessing module, wanda ke ฦ™unshe da kayan aikin da ake buฦ™ata don amfani da sarrafa layi ษ—aya a Python.
  2. ฦ˜irฦ™iri aikin da ake kira square wanda kawai ya yi barci na rabin daฦ™iฦ™a sannan ya dawo da murabba'in hujjar shigarsa. Wannan aikin yana kwatanta lissafin da ke ษ—aukar lokaci mai ma'ana don kammalawa.
  3. ฦ˜irฦ™iri jerin da ake kira lambobin, wanda ya ฦ™unshi lamba daga 0 zuwa 9 (haษ—e).
  4. Qaddamar da a pool abu daga multiprocessing module. Abun Pool yana aiki azaman hanyar sarrafa tsarin ma'aikaci wanda zaku yi amfani da shi don daidaita ayyukanku.
  5. Kira map hanya a kan abin tafkin, kuma shiga cikin square aiki da lambobin jeri. Hanyar taswirar ta yi amfani da aikin murabba'in ga kowane abu a cikin lissafin lambobi a lokaci guda, ta amfani da hanyoyin da ma'aikaci ke samuwa a cikin tafkin.
  6. Buga jerin abubuwan da aka samu na squared_numbers, wanda yakamata ya ฦ™unshi ฦ™imar ฦ™ima daga lissafin lambobi.

Python Multiprocessing Library

Python multiprocessing ษ—akin karatu yana ba da dabarar dabarar aiwatar da daidaito a cikin shirin ku. Yana rufe wasu rikitattun abubuwan da ke da alaฦ™a da shirye-shirye na layi daya ta hanyar ba da babban matakin abstractions kamar pool. Ajin Pool yana sauฦ™aฦ™a rarraba ayyuka a cikin matakai da yawa, yana bawa mai amfani damar samun fa'idodin aiki tare tare da ฦ™aramin matsala.

Python Itertools Module da Ayyuka masu alaฦ™a

Duk da yake multiprocessing shine kyakkyawan bayani ga ayyuka masu kama da juna da yawa, yana da kyau a faษ—i cewa Python kuma yana ba da wasu ษ—akunan karatu da kayan aikin da ke biyan buฦ™atu iri ษ—aya. Tsarin itertools, alal misali, yana ba da ษ—imbin ayyuka waษ—anda ke aiki akan na'urori masu ฦ™arfi, galibi tare da ingantaccen aiki. Wasu itertools suna aiki kamar imap() da kuma imap_unordered() na iya daidaita tsarin aiwatar da aiki zuwa mai iya jujjuyawa. Koyaya, yana da mahimmanci a lura cewa itertools yana mai da hankali ne da farko akan mafita na tushen bayanai, yayin da ษ—akin karatu da yawa yana ba da cikakkiyar hanya don daidaitawa, yana ba da ฦ™arin kayan aiki da ฦ™arfi fiye da ayyukan taswira.

Shafi posts:

Leave a Comment