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)
- Na farko, shigo da multiprocessing module, wanda ke ฦunshe da kayan aikin da ake buฦata don amfani da sarrafa layi ษaya a Python.
- ฦ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.
- ฦirฦiri jerin da ake kira lambobin, wanda ya ฦunshi lamba daga 0 zuwa 9 (haษe).
- 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.
- 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.
- 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.