python multi-task basis

1. Multitasking: two blocks running simultaneously 
2. function to create a thread and start to:
Import Threading
thread name = threading.Thread (target = function name, args = parameter tuple) # create a thread object, not really thread, target
thread name .start () # thread of execution threads at this time to create a real and can be scheduled for execution
. 3. the end of the thread: create a target function is finished when threads
4. the program will have a default main thread execution, the body responsible for the overall program execution, the main thread after the end of the thread to end all child
5. class to create a thread:
the definition of a class inherits from threading.Thread
in which to achieve run method is to put the code thread performed
when you want to create a thread, the first instance of that class (the equivalent of instantiating a thread object, not a real thread), then use the instance name calling start () method (this time is the real os handed over to create a thread scheduling )
6. when the function to modify global variables, whether to use the global point depends on whether the global variables were modified:
if modified To make global variables point to somewhere else, you must use global, otherwise without gloabal
7. When a thread is created when there is objective function parameters, create a thread object if, in addition to target parameters, there args parameter (tuple function parameters)
8. shared global variables multithreaded
9. synchronous and mutually exclusive ways:
by threading.Lock () to instantiate a lock
Respectively before and after the need to add the code lock .acpuire () and a lock .release. ()
Code execution therebetween having atomic (i.e., either performed or not performed)
concept can be understood as the lock semaphore operation system

10. the multi-threaded access to shared resources may lead to a deadlock, solutions:
1. Add a timeout
2. bankers algorithm

difference 11. processes and procedures: simple to understand, a dynamic one static: the state of the process is running + occupied resource

status 12. the process: the three main ready to run blockage
13. multitasking way: threads, processes, coroutines
14. the process creates these steps:

instance name = multipleporcessing.Porcess () to instantiate a process object
instance. start () to create a process and handed over to the OS scheduler

15. the process can be divided into the main points of the child process
16. each creates a process, it will also copy the main program resources to the child, thus creating multiple new process of resource consumption large, when the child process does not change the program code of multiple sub-processes to share a code.
17. The process of simple comparison process and thread is the basic unit of resource allocation, multi-thread is the basic unit of execution.
Function: the first plurality of open e.g. QQ QQ which a plurality of, for example, chat window on a computer


an important way of inter-process communication 18: Queue Queue (program can be decoupled) interprocess data sharing
19. process pool pool pre-create a certain number of processes for dynamic allocation process multiple tasks dynamically to perform tasks to handle an unspecified number of the general situation, and a small number of tasks identified using traditional methods
Features: Repeat the process using the process of doing the task pool
20. The process pool use:
from multiprocessing Import Pool
creation process pool instance instance name = Pool (the number of process pool)

as a function of the distribution process to execute the instance name .apply_async when needed ( function name, the argument tuple) (no need to consider the case of process pool is occupied finished the process of allocating, when needed despite the call statement, the system will automatically manage and implement (automatic blocking and automatic call))

without the need for creating more than pool closed process instance name .close () task when
the main process waits for all child process to complete the instance name .join ()

21. the use of process pool Note: when using the process completes the pool, main pool process will not necessarily wait for the process of the process, to get all pool processes to complete all tasks must be called after the join process pool allocation process is completed () method
22.isinstance (object class) to determine whether an object is an instance of a class.
23. The objects can be simply understood by iteration: may be used for the object to traverse the elements achieved the __iter __ () method
simply understood iterator: the next value can be acquired by an object next () method, the __iter achieved __ () and the __next __ () method of
the iterator is an iterator objects must
24.Custom iterables: __iter__ implement methods in the class, the method returns an iterator object instance of a class is iterable
25. Custom iterator: __next__ implemented method in a class, the return value of each iteration. Iterator does not store all the data, only the method of storing data obtained, and then implement the __iter __ () method returns self
when implementing an iterative manner, and can be implemented separately iterative iterator object may be achieved directly iterators to
26. by iterables of the __iter __ () method to obtain the iterator, call next (iterator returns) to obtain the next iteration cycle value every time
the substance 28. a next (iterables) method; _ incoming call object -next __ () method
29. generator: a special iterator, leaving only the algorithm generates data, not all the stored data can be called and used for next loop method of
two kinds of generator 29. ways:
1. by yield return function value, in which case it would be the function generator creates a function generator generating a variable name = by way function (parameter)
2. generator derivations (with i, for i in equation iterables )

30.yield statement can be assigned to a variable, the variable name = yield variables, by way .send generator (variable) may be provided to copy the contents of a variable yield statement
31. the effect generator : Let function returns the execution result of the statement, and can continue with the remaining statements
32. yield can be achieved by a plurality of tasks performed alternately
33.greenlet is further encapsulated to yield ,, to manage the execution of multiple tasks
34. Efficiency: Co higher than the range above multi-threaded multi-process
35. The general use of gevent implement coroutine

Guess you like

Origin www.cnblogs.com/burningcarbon/p/11220189.html