Код: Выделить всё
try:
nsems_max = os.sysconf("SC_SEM_NSEMS_MAX")
except (AttributeError, ValueError):
# sysconf not available or setting not available
return
if nsems_max == -1:
# indetermined limit, assume that limit is determined
# by available memory only
return
if nsems_max >= 256:
# minimum number of semaphores available
# according to POSIX
return
_system_limited = ("system provides too few semaphores (%d"
" available, 256 necessary)" % nsems_max)
raise NotImplementedError(_system_limited)
Код: Выделить всё
# Process Pool Executor; CPU Bound, many tasks
import concurrent.futures
#from concurrent.futures
# Produce cares
class Care_Bear_Products(object):
def __init__(self, prod_queue):
self.prod_queue = prod_queue
def more_care_bear(self):
print("Gotcha care bear right here!\n")
self.prod_queue.place_product()
# END CARE_BEAR_PRODUCTS CLASS
# Consume cares
class Care_Bear_Consumption(object):
def __init__(self, prod_queue):
self.prod_queue = prod_queue
def get_care_bear(self):
print("Gett'n more care bear!\n")
self.prod_queue.consume_product()
class Product_Queue(object):
def __init__(self):
self.queue = list()
self.serial = 0
def place_product(self):
queue.append(serial)
print("Product {serial} ready!\n")
serial += 1
def consume_product(self):
product = queue[-1]
pop([-1])
print(f"Consumed {product}\n")
# END CARE_BEAR_CONSUMPTION CLASS
# TEST TEST TEST TEST TEST
# Required for multiprocessing to work
#correctly on many systems
prod_queue = Product_Queue()
producer = Care_Bear_Products(prod_queue)
consumer = Care_Bear_Consumption(prod_queue)
with concurrent.futures.ThreadPoolExecutor(max_workers = 10) as executor:
for _ in range(100):
# Produce and Consume
executor.submit(producer.more_care_bear)
executor.submit(consumer.get_care_bear)
executor.shutdown(wait=True)
Подробнее здесь: https://stackoverflow.com/questions/798 ... ws-filenot
Мобильная версия