Re-read "Parallel Programming with Python" and take notes

Source: Internet
Author: User

Concurrent programming, in which language all belong to high-end applications, it must be the only nerve to understand the language.

In the work is not much use, forget some content, slowly look, slowly fill up.

I saw nearly one-third a day today (I think the foreign language is getting faster and quicker?? :)),

Practice the Multi-threaded threading module.

#coding: Utf-8import logging, threadingfrom Queue Import Queuelogger = 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] Fibon ACCI of key [%d] with result [%d] "% (Threading.current_thread (). Name, value, Fibo_dict[value])) def qu Eue_task (condition): Logging.debug (' starting queue_task ... ') with Condition:for item in INPUT_LIST:SHARED_QUEUE.PU T (item) Logging.debug ("notifying Fibonacci_task threads that the queue was ready to consume ...") condition.no Tifyall () threads = [Threading. Thread (Target=fibonacci_task, args= (Queue_condition,)) for I in range (4)][thread.start () for th Read in Threads]prod = threading. Thread (name= "Queue_task_thread", Target=queue_task, args= (Queue_condition,)) Prod.start () [Thread.joi n () for thread in threads]

  

Re-read "Parallel Programming with Python" and take notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.