Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python raspberry pie how to achieve process interaction through the queue program

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article Xiaobian for you to introduce in detail the "python raspberry pie through the queue how to achieve process interaction procedures", detailed content, clear steps, details handled properly, I hope that this "python raspberry pie through the queue how to achieve process interaction procedures" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

Write at the front

Now the shopping cart has a task requirement, that is, image recognition and motion control are needed, so two processes need to be initialized to complete the corresponding actions respectively. Because motion control needs the result of image recognition, it now involves python syntax to realize the cooperation between the two processes. This blog combines the actual python program to realize the process interaction through the queue.

Program analysis

First, let's introduce the library functions we need:

Import timefrom multiprocessing import Process, Queue

From multiprocessing import Process and Queue are the main libraries that provide queue and process operation functions. The process and queue operation functions we used in this test are:

Q1.get () # get the content in Q1 queue q1.put ("Q1 put things1") # add content to Q1 queue P1 = Process (target=test1, args= (Q1, Q2)) # initialize p1 process p1.start () # start p1 process p1.join () # add p1 to system scheduling q1.qsize () # check the size of Q1 queue

The correspondence between the thread and the process in the test:

Q1 queue-> Q1 process Q2 queue-> Q2 process

After having the above basic functions, we can see the implementation of our program. We can see that the process is initialized, started and added to the system scheduling in the program. After running these statements, we can think that the p1 and p2 processes have been initialized.

P1 = Process (target=test1, args= (Q1, Q2)) p2 = Process (target=test2, args= (Q1, Q2)) p1.start () p2.start () p1.join () p2.join ()

After the process is initialized, you can take a look at the main test functions, starting with test1

Def test1 (Q1, Q2): q1.put ("fisrt data") while (1): s = q2.get () print ("Q2 left +" + str (q2.qsize () print ("Q1 get +" + s) q1.put ("Q1 put things1") q1.put ("Q1 put things2") time.sleep (1)

In the previous initialization function, we initialized the p1 process first, that is, test1 will run first, so in order to ensure that there is content in the queue of the process at the beginning, I first added the content "fisrt data" to the Q1 queue at the beginning of the function. In order to ensure that the test continues, I set the test1 process to a dead loop. In test1, I first get the contents of the Q2 queue and the Q2 queue size and print it. Then the new content will be put into the Q1 queue. Notice that there are two nodes of data, namely "Q1 put things1" and "Q1 put things2".

Here I would like to ask a question:

That is, when the p1 process goes to get the contents of the Q2 queue, will the p1 process take out all the contents of the Q2 queue or only the data at the front of the Q2 queue?

Next, let's solve this problem. Next, let's take a look at what is done in the test2 function:

Def test2 (Q1, Q2): while (1): s = q1.get () print ("Q1 left +" + str (q1.qsize () print ("Q2 get +" + s) q2.put ("Q2 put data1") time.sleep (1)

Less work is done in the test2 function, that is, first get the data in the Q1 queue and the remaining queue size of Q1 and print it, and then add "Q2 put data1" to the Q2 queue.

Result analysis

Let's take a look at the calls in the main function:

If _ _ name__ = = "_ _ main__": P1 = Process (target=test1, args= (Q1, Q2)) p2 = Process (target=test2, args= (Q1, Q2) p1.start () p2.start () p1.join () p2.join ()

In the main function, you just start the thread. Let's take a look at the output:

From the results, we can clearly see that the Q1 thread will add 2 node data at a time, and then the p2 process will q1.get () from the Q1 thread. With the continuous increase of the number of cycles, we can find that the length of the Q1 queue continues to increase, so we can draw a conclusion:

When the p1 process gets the contents of the Q2 queue, the p1 process will take out the data at the front of the Q2 queue.

Read this, the "python raspberry pie through the queue how to achieve process interaction procedures" article has been introduced, want to master the knowledge of this article still need to practice and use in order to understand, if you want to know more related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report