再读《Parallel Programming with Python》并作笔记

简介: 并发编程,在哪个语言里都属于高端应用,一定得会了才好意思说懂了这门语言。 在工作中用得并不是很多,忘了一些内容,就慢慢看,慢慢补上。 今天一天看了近三分之一(我看外文越来越快了??:)), 实践一下多线程的threading模块。

并发编程,在哪个语言里都属于高端应用,一定得会了才好意思说懂了这门语言。

在工作中用得并不是很多,忘了一些内容,就慢慢看,慢慢补上。

今天一天看了近三分之一(我看外文越来越快了??:)),

实践一下多线程的threading模块。

#coding: utf-8

import logging, threading
from Queue import Queue

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(message)s')

ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)

fibo_dict = {}
shared_queue = Queue()
input_list = [30, 10, 55, 71]

queue_condition = threading.Condition()

def fibonacci_task(condition):
    with condition:
        while shared_queue.empty():
            logger.info("[%s] - waiting for elements in queue..."
                        % threading.current_thread().name)
            condition.wait()
        else:
            value = shared_queue.get()
            a, b = 0, 1
            for item in range(value):
                a, b = b, a + b
                fibo_dict[value] = a
        shared_queue.task_done()
        logger.debug("[%s] fibonacci of key [%d] with result [%d]" %
                     (threading.current_thread().name, value, fibo_dict[value]))

def queue_task(condition):
    logging.debug('Starting queue_task...')
    with condition:
        for item in input_list:
            shared_queue.put(item)
        logging.debug("Notifying fibonacci_task threads that the queue is ready to consume...")
        condition.notifyAll()

threads = [threading.Thread(target=fibonacci_task,
                            args=(queue_condition,)) for i in range(4)]
[thread.start() for thread in threads]


prod = threading.Thread(name="queue_task_thread", target=queue_task,
                        args=(queue_condition,))
prod.start()

[thread.join() for thread in threads]

  

目录
相关文章
|
机器学习/深度学习 算法 Serverless
【李宏毅机器学习CP4】(task2)回归+Python Basics with Numpy
第一部分:回归栗子 ps:CP3的部分在上一篇笔记中【李宏毅机器学习】CP1-3笔记了。 1.问题描述 现在假设有10个x_data和y
154 0
【李宏毅机器学习CP4】(task2)回归+Python Basics with Numpy
|
Java 关系型数据库 MySQL
Python高级语法5:私有属性、魔法属性、with与上下文管理器
Python高级语法5:私有属性、魔法属性、with与上下文管理器
173 0
|
存储 数据库 Python
Python - with 语句
Python - with 语句
89 0
|
数据库连接 数据库 Python
Python中的With语句
在Python中,您需要通过打开文件来访问文件。您可以使用 open()函数来实现。Open 返回一个文件对象,该文件对象具有用于获取有关已打开文件的信息和对其进行操作的方法和属性。
133 0
|
缓存 Python
Python - with open()、os.open()、open()的详细使用
Python - with open()、os.open()、open()的详细使用
487 0
|
程序员 Python
说说Python中with的用法?
公众号新增加了一个栏目,就是每天给大家解答一道Python常见的面试题,反正每天不贪多,一天一题,正好合适,只希望这个面试栏目,给那些正在准备面试的同学,提供一点点帮助!
96 0
|
Python
python with as有什么好处?
python with as有什么好处?
84 0
|
关系型数据库 MySQL 数据库连接
With关键字的使用 | 手把手教你入门Python之七十八
with语句实质上是⼀个上下⽂管理器,很多需要手动关闭的连接,比如说,文件连接,socket连接,数据库的连接都能使用with关键字来自动关闭连接。
|
C++ Python 编译器
为什么Python没有属性赋值的“with”语句?
Python有一个 'with' 语句,它封装了块的执行,在块的入口和出口调用代码。有些语言的结构是这样的: a = 1 # equivalent to obj.a = 1 total = total + 1 # obj.total = obj.total + 1 在Python中,这样的结构是不明确的。
|
Python
python:好用的 with 语法
python:好用的 with 语法
1379 0

热门文章

最新文章