ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python: threading.Semaphore & threading.BoundedSemaphore & GIL

2022-02-25 22:01:44  阅读:198  来源: 互联网

标签:__ logging Python self datetime threading semaphore GIL


 

 

import threading, time, logging, random

FORMAT = '%(asctime)-15s %(process)d %(lineno)-3s [%(threadName)-11s %(thread)6d] %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)


def vacuity(semaphore: threading.Semaphore):
    logging.info(f'before semaphore.acquire()')
    logging.error(semaphore.acquire(blocking=True))
    logging.critical(f'after semaphore.acquire()')


semaphore = threading.Semaphore(3)
logging.info(semaphore.acquire())
logging.info(semaphore.acquire())
logging.info(semaphore.acquire())

threading.Thread(target=vacuity, args=(semaphore,)).start()
logging.info('~' * 60)
logging.info(semaphore.acquire(blocking=False))
logging.info(semaphore.acquire(timeout=2))

logging.info(f'semaphore.release {semaphore.release()}')

 

 

 

 

import threading, time, logging, random

FORMAT = '%(asctime)-15s %(process)d %(lineno)-3s [%(threadName)-11s %(thread)6d] %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)


class Connection:
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return f'Connection: {self.name}'

    __str__ = __repr__


class Pool:
    def __init__(self, count):
        self.count = count
        self.__semaphores = threading.Semaphore(count)
        self.pool = [Connection(f'Connection-{b}') for b in range(self.count)]

    def get(self):
        # if len(self.pool) > 0:
        #     time.sleep(0.0001)
        #     return self.pool.pop()
        # else:
        #     return None
        self.__semaphores.acquire(blocking=True)
        return self.pool.pop()

    def revert(self, connection: Connection):
        self.pool.append(connection)
        self.__semaphores.release()  # 必须在后, 在前可能, 刚执行完, 就进程切换


p = Pool(3)


def get_revert(pool: Pool):
    connection = pool.get()
    logging.info(f'get {connection}')
    threading.Event().wait(random.randint(1, 5) / 1000)
    pool.revert(connection)


for b in range(100):
    threading.Thread(name=f'Thread-{b}', target=get_revert, args=(p,)).start()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

import threading, time, logging, random, datetime

FORMAT = '%(asctime)-15s %(process)d %(lineno)-3s [%(threadName)-11s %(thread)6d] %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)

commence = datetime.datetime.now()


def calc():
    sum = 0
    for _ in range(1000000000):
        sum += 1


calc()
calc()
calc()
calc()
calc()

delta = (datetime.datetime.now() - commence).total_seconds()

logging.error(delta)

单进程单线程 四核cpu Utilization Rate: 25%

 

 

import threading, time, logging, random, datetime

FORMAT = '%(asctime)-15s %(process)d %(lineno)-3s [%(threadName)-11s %(thread)6d] %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)


def calc():
    sum = 0
    for _ in range(1000000000):
        sum += 1


commence = datetime.datetime.now()

vails = []

for _ in range(5):
    t = threading.Thread(target=calc())
    t.start()
    vails.append(t)

for vail in vails:
    vail.join()

delta = (datetime.datetime.now() - commence).total_seconds()

logging.error(delta)

单进程多线程
利用率 25%

 

标签:__,logging,Python,self,datetime,threading,semaphore,GIL
来源: https://www.cnblogs.com/dissipate/p/15937918.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有