मैं सोच रहा था कि कैसे मैं भी एक समय में केवल 10 धागे का उपयोगपायथन थ्रेड सीमा कैसे सेट करें?
with open("data.txt") as f:
for line in f:
lines = line.rstrip("\n\r")
t1 = Thread(target=Checker, args=("company"))
t1.start()
मैं सोच रहा था कि कैसे मैं भी एक समय में केवल 10 धागे का उपयोगपायथन थ्रेड सीमा कैसे सेट करें?
with open("data.txt") as f:
for line in f:
lines = line.rstrip("\n\r")
t1 = Thread(target=Checker, args=("company"))
t1.start()
उपयोग पायथन के ThreadPoolExecutor max_workers साथ तर्क करने के लिए 10
कुछ इस तरह सेट pool.submit()
में फ़ंक्शन का नाम है, तर्क आसानी से अल्पविराम से अलग मूल्यों के रूप में पारित किए जाते हैं।
pool.shutdown(wait=True)
निष्पादन को पूरा करने के लिए सभी धागे के लिए इंतजार कर रहा है।
ThreadPoolExecutor
का उपयोग करें और यह बताना है कि आप 10 धागे चाहते कुछ इस तरह सीमित हो सकती है।
def your_function_processing_one_line(line):
pass # your computations
with concurrent.futures.ThreadPoolExecutor(10) as executor:
result = executor.map(your_function_processing_one_line, [line for line in f])
... और आपके पास result
में सभी परिणाम होंगे। `
pool = ThreadPoolExecutor(max_workers=10)
with open("data.txt") as f:
for line in f:
lines = line.rstrip("\n\r")
pool.submit(Checker,"company")
pool.shutdown(wait=True)
pool
10. के रूप में की जरूरत है स्वचालित रूप से धागे आवंटित करेगा, आवंटन की अधिकतम संख्या को सीमित पहला तर्क:
क्या होगा एकाधिक पैरा? –
यह भी संभव है। [यह अच्छा जवाब] पर एक नज़र डालें (https://stackoverflow.com/questions/6785226/pass-multiple-parameters-to-concurrent-futures-executor-map/6976772#6976772)। – yogabonito
उपयोग threadPool
multiprocessing
मॉड्यूल से (दोनों अजगर 2.6+ और अजगर 3 के लिए):
from multiprocessing.pool import ThreadPool
केवल एक चीज है कि यह अच्छी तरह से प्रलेखित नहीं है ...
मैं धागे को एक चर में टोपी करने के लिए इस नेस्टेड लूप को लिखा था। यह कोड प्रक्रिया के लिए आदेशों की एक प्रीसेट सरणी पर निर्भर करता है। मैंने थ्रेड लॉन्च के लिए अन्य उत्तरों से कुछ तत्व उधार लिया है।
import os, sys, datetime, logging, thread, threading, time
from random import randint
# set number of threads
threadcount = 20
# alltests is an array of test data
numbertests = len(alltests)
testcounter = numbertests
# run tests
for test in alltests:
# launch worker thread
def worker():
"""thread worker function"""
os.system(command)
return
threads = []
t = threading.Thread(target=worker)
threads.append(t)
t.start()
testcounter -= 1
# cap the threads if over limit
while threading.active_count() >= threadcount:
threads = threading.active_count()
string = "Excessive threads, pausing 5 secs - " + str(threads)
print (string)
logging.info(string)
time.sleep(5)
# monitor for threads winding down
while threading.active_count() != 1:
threads = threading.active_count()
string = "Active threads running - " + str(threads)
print (string)
logging.info(string)
time.sleep(5)
बस धागे को जीवित और अवरुद्ध करें जब तक उनमें से 10 उनमें से 10 या उससे अधिक न हो जाएं। – ForceBru
संभावित डुप्लिकेट [मैं पाइथन में सक्रिय धागे की संख्या को कैसे सीमित करूं?] (Http://stackoverflow.com/questions/1787397/how-do-i-limit-the-number-of-active-threads-in -पीथॉन) – zondo
मल्टीप्रोसेसिंग पूल के समान [पायथन थ्रेड पूल का संभावित डुप्लिकेट?] (http://stackoverflow.com/questions/3033952/python-thread-pool-similar-to-the-multiprocessing-pool) –