Python third-party library series 12-multithreaded Threading Library

Source: Internet
Author: User

One foot on two ships, in the program can be achieved. Use Python multithreading concurrency to implement multiple threads in parallel. You can even step on n boat.

#-*-coding:utf-8-*-Import Threading Class Mythread (object): Def __init__ (self, func_list=none): #所有线程函数的返回值app End self.reps = List () Self.func_list = Func_list Self.threads = [] def set_thread_func_list ( Self, Func_list: "" "@note: Func_list is a list, each element is a dict, has Func and args two parameters" "" Self.func_lis
        t = func_list def start (self): "" @note: Start multithreaded execution and block to end "" "self.threads = [] Self.reps = List () for func_dict in Self.func_list:if func_dict["args"]: New_arg_li
                    st = [] New_arg_list.append (func_dict["func"]) for Arg in func_dict["args"]: New_arg_list.append (arg) new_arg_tuple = tuple (new_arg_list) T = Threading. Thread (Target=self.trace_func, args=new_arg_tuple) Else:t = Threading. Thread (Target=self.trace_func, args= (func_dict["func"),) Self.threads.append (t) for Thread_obj in Self.threads:thread_obj.start () for T
        Hread_obj in Self.threads:thread_obj.join () def ret_value (self): "" @note: return value of all thread functions "" "Return Self.reps def trace_func (self, func, *args, **kwargs):" "@note: Alternative Profile_f
        UNC, a new trace thread returns a function of the value, a function of the thread function that is actually executing, to get the return value "" "ret = func (*args, **kwargs) self.reps.append (ret) Return self.reps import Time def send [result, id]: If ID in (1,2,4): Time.sleep (id*2) result["ERR
    OR "].append (ID) else:result[" OK "].append (ID) print" id:%s, result:%s "% (ID, str (result)) def test (): Print "start Test" result = {"OK": [], "ERROR": []} g_func_list = [] mt = Mythread () to I in range (1 5): G_func_list.append ({"Func": Send, "args": (Result, i)}) mt.set_thread_func_list (g_func_list) Mt.start (

) print "End Test"
if __name__ = = "__main__": Test () 

But just read the "Python Source Analysis" 15th chapter multithreading mechanism, in fact Python in order to implement multithreading mechanism, using the Gil (Global interpreter Lock) lock to control multithreading synchronization, so:

1. multi-core is useless for Python because only one thread at a time acquires the lock and gets the right to run the Python virtual machine.

2. Python multi-Threading scheduling mechanism: (Otherwise the thread becomes a serial run)

(1) After running 100 instructions, schedule the next thread

(2) which thread is scheduled to run by the operating system (which is why the Python thread is the native thread)

(3) Congestion scheduling (encounter sleep, etc.) does not reset 100 values

3. The Gil is not turned on by default, and only the user can invoke multithreading to generate this Gil lock.

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.