मल्टीथ्रेडिंग अवधारणाएं
मल्टीथ्रेडिंग लगभग सभी आधुनिक प्रोग्रामिंग भाषाओं की मूल अवधारणा है, विशेष रूप से अजगर के धागे के सरलीकृत कार्यान्वयन के कारण।
एक थ्रेड एक प्रोग्राम के भीतर एक उप-प्रोग्राम है जिसे कोड के अन्य अनुभाग से स्वतंत्र रूप से निष्पादित किया जा सकता है। एक थ्रेड एक ही संदर्भ साझा करने वाले प्रोग्राम के मेमोरी जैसे चलने योग्य संसाधनों में निष्पादित होता है।
जब एक ही प्रक्रिया में, हम एक साथ कई थ्रेड्स निष्पादित कर रहे होते हैं, तो इसे मल्टीथ्रेडिंग कहा जाता है।
थ्रेड कार्यान्वयन के लिए पायथन मल्टीथ्रेडिंग मॉड्यूल
कार्यक्रमों में धागे को लागू करने के लिए, पायथन दो मॉड्यूल प्रदान करता है -
- थ्रेड (पायथन 2.x के लिए) या _थ्रेड (पायथन 3.x के लिए) मॉड्यूल
- थ्रेडिंग मॉड्यूल
जहां थ्रेड मॉड्यूल फ़ंक्शन के रूप में थ्रेड बनाता है जबकि थ्रेडिंग मॉड्यूल थ्रेड बनाने के लिए ऑब्जेक्ट-ओरिएंटेड दृष्टिकोण प्रदान करता है।
सिंटैक्स
_thread.start_new_thread(func, args[, kwargs])
ऊपर एक नया धागा शुरू होता है और इसकी पहचानकर्ता देता है। पहला तर्क एक फ़ंक्शन func है जिसे थ्रेड दूसरे तर्क के साथ निष्पादित करता है जिसमें तर्कों की स्थितिगत सूची के साथ एक टपल होता है। वैकल्पिक kwargs तर्क कीवर्ड तर्कों का एक शब्दकोश निर्दिष्ट करता है। जब फ़ंक्शन वापस आता है, तो थ्रेड चुपचाप मौजूद रहता है।
इसमें, हम क्लाइंट-सर्वर एप्लिकेशन का एक मूल उदाहरण देखते हैं। जहां क्लाइंट मूल रूप से सॉकेट कनेक्शन खोलते हैं और सर्वर को क्वेरी भेजते हैं। सर्वर वापस प्रतिक्रिया करता है।
बिना किसी तर्क के चलने पर, यह प्रोग्राम एक TCP सॉकेट सर्वर से शुरू होता है जो पोर्ट 8000 पर 127.0.0.1 के कनेक्शन को सुनता है।
client_thread1.py
import socket
import sys
def main():
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 8000
try:
soc.connect((host, port))
except:
print("Connection Error")
sys.exit()
print("Please enter 'quit' to exit")
message = input(" -> ")
while message != 'quit':
soc.sendall(message.encode("utf8"))
if soc.recv(5120).decode("utf8") == "-":
pass # null operation
message = input(" -> ")
soc.send(b'--quit--')
if __name__ == "__main__":
main() जबकि सर्वर प्रोग्राम है,
server_thread1.py
import socket
import sys
import traceback
from threading import Thread
def main():
start_server()
def start_server():
host = "127.0.0.1"
port = 8000 # arbitrary non-privileged port
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("Socket created")
try:
soc.bind((host, port))
except:
print("Bind failed. Error : " + str(sys.exc_info()))
sys.exit()
soc.listen(6) # queue up to 6 requests
print("Socket now listening")
# infinite loop- do not reset for every requests
while True:
connection, address = soc.accept()
ip, port = str(address[0]), str(address[1])
print("Connected with " + ip + ":" + port)
try:
Thread(target=client_thread, args=(connection, ip, port)).start()
except:
print("Thread did not start.")
traceback.print_exc()
soc.close()
def clientThread(connection, ip, port, max_buffer_size = 5120):
is_active = True
while is_active:
client_input = receive_input(connection, max_buffer_size)
if "--QUIT--" in client_input:
print("Client is requesting to quit")
connection.close()
print("Connection " + ip + ":" + port + " closed")
is_active = False
else:
print("Processed result: {}".format(client_input))
connection.sendall("-".encode("utf8"))
def receive_input(connection, max_buffer_size):
client_input = connection.recv(max_buffer_size)
client_input_size = sys.getsizeof(client_input)
if client_input_size > max_buffer_size:
print("The input size is greater than expected {}".format(client_input_size))
decoded_input = client_input.decode("utf8").rstrip()
result = process_input(decoded_input)
return result
def process_input(input_str):
print("Processing the input received from client")
return "Hello " + str(input_str).upper()
if __name__ == "__main__":
main() स्क्रिप्ट के ऊपर चलने पर, टर्मिनल में server_thread1.py इस रूप में चलाएँ,
python server_thread1.py Socket created Socket now listening
हम सर्वर विंडो देखेंगे और प्रवाह को समझेंगे। अब एकाधिक क्लाइंट टर्मिनल खोलें, क्लाइंट थ्रेड चलाएँ
python client_thread1.py Enter 'quit' to exit -> Zack ->
दूसरे टर्मिनल में, दूसरा क्लाइंट प्रोग्राम चलाएं और सर्वर टर्मिनल विंडो भी देखें,
python client_thread1.py Enter 'quit' to exit -> Python -> quit
एक और टर्मिनल, क्लाइंट थ्रेड चलाएँ,
python client_thread1.py Enter 'quit' to exit -> world! -> Anothny ->
और हम देख सकते हैं कि हमारी सर्वर विंडो कुछ इस तरह आउटपुट प्रदर्शित करेगी,
Socket created Socket now listening Connected with 127.0.0.1:50275 Processing the input received from client Processed result: Hello ZACK Connected with 127.0.0.1:50282 Processing the input received from client Processed result: Hello PYTHON Processing the input received from client Client is requesting to quit Connection 127.0.0.1:50282 closed Connected with 127.0.0.1:50285 Processing the input received from client Processed result: Hello WORLD! Processing the input received from client Processed result: Hello ANOTHNY
तो थ्रेड एकाधिक सॉकेट कनेक्शन और क्लाइंट को संभालने के लिए सबसे आम तकनीक में से एक प्रदान करते हैं।