# practice :
import time
from multiprocessing import Pool
def run(fn):
#fn: A function parameter is an element of a data list
return fn * fn if __name__ == "__main__":
testFL = [1,2,3,4,5,6]
print 'Single process execution sequence:' # Sequential execution ( Serial execution , Single process )
s = time.time()
for fn in testFL:
run(fn) e1 = time.time()
print u" Sequence execution time :", int(e1 - s) print 'concurrent:' # Create multiple processes , Parallel execution
pool = Pool(5) # Create ownership 5 Process pool of number of processes
#testFL: List of data to process ,run: Handle testFL Functions for the data in the list
rl =pool.map(run, testFL)
pool.close()# Close process pool , No more new assignments
pool.join()# The main process is blocked waiting for the child process to exit
e2 = time.time()
print u" Parallel execution time :", int(e2 - e1)
print rl # practice :
from multiprocessing import Process, Queue
import os, time, random # Write code executed by data process :
def write(q):
for value in ['A', 'B', 'C']:
print 'Put %s to queue...' % value
time.sleep(random.random()) # Read code executed by data process
def read(q):
while not q.empty():
# if not q.empty():
print 'Get %s from queue.' % q.get(True)
time.sleep(1) # The goal is to wait for the write queue to complete if __name__=='__main__':
# Parent process creation Queue, And passed to all subprocesses
q = Queue()
pw = Process(target = write, args = (q,))
pr = Process(target = read, args = (q,))
# Start subprocess pw, write in :
# Start subprocess pr, Read :
# wait for pw end :
print “Done!”

【Python】 process 3 More articles about

  1. python—— Process foundation

    We all know now python Multithreading is a hole , So many processes become necessary at this time . Multi process realizes multi CPU The use of , The efficiency is fantastic ~~~ Have a multi process program : #!/usr/bin/env python #- ...

  2. Use gdb debugging Python process

    Use gdb debugging Python process Sometimes we want to debug a running Python process , Or a Python Process coredump. For example, now I come across a mod_wsgi The process is dead , Don't accept the request , Want to see if it's running to ...

  3. python process 、 Threads 、 coroutines ( Reprint )

    python Thread and process introduction The history of processes and threads We all know that computers are made up of hardware and software . In hardware CPU It's the core of the computer , It undertakes all the tasks of the computer . The operating system is the software running on the hardware , It's the computer manager , It's responsible for ...

  4. Python process 、 Threads 、 Explain in detail

    The history of processes and threads We all know that computers are made up of hardware and software . In hardware CPU It's the core of the computer , It undertakes all the tasks of the computer . The operating system is the software running on the hardware , It's the computer manager , It is responsible for the management and distribution of resources . Task scheduling . ...

  5. python Process pool profiling ( One )

    python The two modules commonly used to handle processes in are subprocess and multiprocessing, among subprocess Usually used to execute external programs , For example, some third-party applications , instead of Python Program . if necessary ...

  6. python—— process 、 Threads 、 coroutines

    Python Threads Threading Used to provide thread-related operations , A thread is the smallest unit of work in an application . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/usr/bin/env pytho ...

  7. python/ Summary of process threads

    python/ Summary of process threads One . Description of process and thread : process : The smallest resource management unit Threads : Minimum execution unit When a process is executed, a thread is executed by default ( The main thread ) How processes and threads work : Serial : If we have A.B.C Mission ...

  8. python Process introduction The process is simple to use join Verify space isolation

    One . Multiprogramming Technology ( Details refer to :https://www.cnblogs.com/clschao/articles/9613464.html) So called multiprogramming technology , It means to allow multiple programs to enter memory and run at the same time ...

  9. Python Sharing data between processes

    Recently, we encountered the problem of data sharing among multiple processes , I checked several blogs on the Internet, which are very good , Record it for future reference . One .Python multiprocessing Cross process object sharing   stay mp In the library , There are three ways to share objects across processes , The first one is ...

  10. python process 、 Multi process

    process : process (Process) A program in a computer is about a running activity on a data set , Is the system resource allocation and scheduling of the basic unit , Is the foundation of the operating system architecture . In early process oriented computer architecture , A process is the basic execution entity of a program : When ...

Random recommendation

  1. Android Source code ——Broadcast Receiver

    Android Source code analysis —— radio broadcast Broadcasting is a way of messaging between components . The implementation of broadcast mechanism is based on Binder Interprocess communication . Binder The difference between interprocess communication and broadcasting :Binder in ,Client Components and Servic ...

  2. Use today bootstrap Medium tagsinput Control , There's a little problem

    When I use it directly , Backstage keeps reporting Uncaught Can't add objects when itemValue option is not set error , The code is as follows : <div> <inp ...

  3. Machine Learning Learning notes (1) —— Linear regression and logical regression

    This series of articles is allowed to reprint , Please keep the full text ! [ Please read first ][ explain & General catalogue ]http://www.cnblogs.com/tbcaaa8/p/4415055.html 1. Gradient descent method (Gradien ...

  4. PHP PDO select Statement result row count

    PDO There is a function PDOStatement::rowCount Back to previous SQL The number of lines affected by the statement . rowCount Function for DELETE, INSERT, perhaps UPDATE The result of the statement is correct , But for the sele ...

  5. iOS SDK: Preview and open the document

    iOS The sandbox in can make the platform more secure , This is also the main benefit of sandbox to users . However, due to the strict restrictions of sandbox , It is troublesome to share data between programs . Generally, documents can be shared between programs through UIDocumentInteractionCon ...

  6. Uva 11029 Leading and Trailing ( seek n^k front 3 Position and back 3 position )

    The question : Here you are. n and k , Let you please n^k The top three and the bottom three Ideas : The last three are simple , Direct fast power is good , The point is how to find the top three , Pay attention to the lead 0 Information : seek n^k Before m position Blog connection address Code : #include < ...

  7. Top 10 common Python development tool

    According to the statistics of authoritative organizations ,Python The demand for talent is as high as 5000+, But at the moment the market will Python Few programmers , Little competition , It's easy to get high paying jobs quickly . Maybe you don't know the top ten Python What are the development tools , Now tell me ...

  8. hold button Put the Chinese Ellipsis at the back

    butt.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail; Add this sentence to it ,uibutton Yes uilabel Methods

  9. zabbix Templates

    https://github.com/xm-y/zabbix-community-repos  https://monitoringartist.github.io/zabbix-searcher/ 

  10. adopt Cookie Skip login captcha 【 limit cookie It doesn't work 】

    Verification Code , Believe that everyone writes web For students of automated testing , It's all a headache , What shall I do? ? There are ways , Let's start with today's way , adopt cookie Bypass login verification code Ideas : You need to capture your login through the packet capture tool cookie Let's start ...