久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <small id='Xrfbe'></small><noframes id='Xrfbe'>

    <legend id='Xrfbe'><style id='Xrfbe'><dir id='Xrfbe'><q id='Xrfbe'></q></dir></style></legend><tfoot id='Xrfbe'></tfoot>

    <i id='Xrfbe'><tr id='Xrfbe'><dt id='Xrfbe'><q id='Xrfbe'><span id='Xrfbe'><b id='Xrfbe'><form id='Xrfbe'><ins id='Xrfbe'></ins><ul id='Xrfbe'></ul><sub id='Xrfbe'></sub></form><legend id='Xrfbe'></legend><bdo id='Xrfbe'><pre id='Xrfbe'><center id='Xrfbe'></center></pre></bdo></b><th id='Xrfbe'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Xrfbe'><tfoot id='Xrfbe'></tfoot><dl id='Xrfbe'><fieldset id='Xrfbe'></fieldset></dl></div>

        • <bdo id='Xrfbe'></bdo><ul id='Xrfbe'></ul>

        input() 正在阻止進(jìn)程的使用

        input() is blocking the use of processes(input() 正在阻止進(jìn)程的使用)

              <legend id='7mYDc'><style id='7mYDc'><dir id='7mYDc'><q id='7mYDc'></q></dir></style></legend>

                <bdo id='7mYDc'></bdo><ul id='7mYDc'></ul>
              • <i id='7mYDc'><tr id='7mYDc'><dt id='7mYDc'><q id='7mYDc'><span id='7mYDc'><b id='7mYDc'><form id='7mYDc'><ins id='7mYDc'></ins><ul id='7mYDc'></ul><sub id='7mYDc'></sub></form><legend id='7mYDc'></legend><bdo id='7mYDc'><pre id='7mYDc'><center id='7mYDc'></center></pre></bdo></b><th id='7mYDc'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='7mYDc'><tfoot id='7mYDc'></tfoot><dl id='7mYDc'><fieldset id='7mYDc'></fieldset></dl></div>
                  <tbody id='7mYDc'></tbody>

                <tfoot id='7mYDc'></tfoot>

                  <small id='7mYDc'></small><noframes id='7mYDc'>

                  本文介紹了input() 正在阻止進(jìn)程的使用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我遇到了多處理問題.如果我在線程中等待輸入,則進(jìn)程不會啟動.

                  I have a problem with multiprocessing. If I am waiting for input in a thread the process is not starting.

                  將輸入放入后臺隊列的類:

                  The class to put the input into a queue in the background:

                  class InputCatcher(Thread):
                      def __init__(self, input_queue):
                          Thread.__init__(self)
                          self.input_queue = input_queue
                  
                      def run(self):
                          while True:
                              self.input_queue.put(input()) # <<-- Without this it works!
                  

                  不會開始的類:

                  class Usb(Process):
                      def __init__(self, port, ctrl=Controller()):
                          Process.__init__(self)
                          self.usb_port = port
                          self.ctrl = ctrl
                  
                  
                      def run(self):
                          self.ctrl.usb_ports.append(self.usb_port)
                          ser = Serial(self.usb_port, 115200)
                          while True:
                              dsl = ser.readline()
                              self.ctrl.get_dataset_queue().put(['USBDS', dsl])
                              print(dsl)
                  

                  開始于:

                  ic = InputCatcher(self.input_queue)
                  ic.setDaemon(True)
                  ic.start()
                  
                  usbs = []
                  for port in usb_ports():
                      if not port in ctrl.xbee_ports:
                          usbs.append(Usb(port, ctrl))
                  
                  for usb in usbs:
                      usb.daemon = True
                      usb.start()
                  

                  推薦答案

                  當(dāng)你調(diào)用 input 時,它阻塞了整個 Python 進(jìn)程,而不僅僅是它運(yùn)行的線程.發(fā)生這種情況是因為從與從任何其他類似文件的對象中讀取一樣,STDIN 涉及進(jìn)行阻塞系統(tǒng)調(diào)用 - 即 input 阻塞以等待用戶輸入發(fā)生在操作系統(tǒng)級別,而不是在 Python 自己的線程管理代碼內(nèi)部.Python 線程對 OS 進(jìn)程調(diào)度程序本質(zhì)上是不可見的,因此 Python 本身 會被阻塞.

                  When you call input, it is blocking the entire Python process, not just the thread it runs in. This happens because reading from STDIN, like reading from any other file-like object, involves making a blocking syscall - that is, input blocking to wait for user input happens at the OS level, rather than inside Python's own thread management code. Python threads are essentially invisible to the OS process scheduler, so Python itself gets blocked.

                  解決此類阻塞問題的常用方法是使用進(jìn)程而不是線程.如果你把 InputCatcher 變成一個進(jìn)程而不是線程,那么它就變成了一個獨立的操作系統(tǒng)級進(jìn)程,操作系統(tǒng)可以獨立調(diào)度,因此系統(tǒng)調(diào)用只會阻塞 那個 進(jìn)程而不是主進(jìn)程.

                  The usual way around blocking problems like this is to use processes instead of threads. If you make InputCatcher into a process rather than a thread, then it becomes a separate OS-level process that the OS can schedule independently, and so the syscall will only block that process and not the main one.

                  除了,Python 會自動在您生成進(jìn)程時關(guān)閉 STDIN.

                  Except, that Python automatically closes STDIN when you spawn a process.

                  因此,您需要在主進(jìn)程中擁有隊列的生產(chǎn)者,而在另一個進(jìn)程中只有消費(fèi)者.這也是一個微不足道的適應(yīng) - 不要啟動生產(chǎn)者 (InputCatcher) 直到所有消費(fèi)者進(jìn)程已經(jīng)產(chǎn)生之后運(yùn)行.這涉及到移動線路:

                  So, you would need to have the producer for the queue in the main process, and only the consumer in another one. This is also a trivial adaption - don't start the producer (InputCatcher) running until after all of the consumer processes have spawned. That involves moving the line:

                  ic.start()
                  

                  到下面兩個循環(huán).但在這種情況下,根本不需要將其作為背景 - 它不會與其他事物同時運(yùn)行.因此,您可以完全忘記 InputCatcher 類,只需像這樣編寫代碼:

                  to below the two loops. But in this case, there's no need for that to be backgrounded at all - it doesn't run simultaneously with other things. So, you can forget about the InputCatcher class entirely, and just write your code like this:

                  for usb in usbs:
                      usb.daemon = True
                      usb.start()
                  
                  while True:
                      input_queue.put(input())
                  

                  您可能還想考慮一個特定的輸入——比如空字符串——來結(jié)束程序.在主運(yùn)行中輸入,只需結(jié)束循環(huán)就可以輕松完成:

                  You might also want to consider a particular input - say, the empty string - to end the program. Having the input in the main run makes this really easy by just ending the loop:

                  while True:
                      data = input('Type data; or enter to exit: ')
                      if not data:
                          break
                      input_queue.put(data)
                  

                  這篇關(guān)于input() 正在阻止進(jìn)程的使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數(shù)傳遞給 pool.map() 函數(shù))
                  multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEncodingError: TypeError(cannot serialize _io.BufferedReader object,)) - IT屋-程序員軟件開
                  Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多進(jìn)程池.當(dāng)其中一個工作進(jìn)程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數(shù)?)
                  yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯誤的另一個混淆,“模塊對象沒有屬性“f)
                    • <i id='Vu2Sd'><tr id='Vu2Sd'><dt id='Vu2Sd'><q id='Vu2Sd'><span id='Vu2Sd'><b id='Vu2Sd'><form id='Vu2Sd'><ins id='Vu2Sd'></ins><ul id='Vu2Sd'></ul><sub id='Vu2Sd'></sub></form><legend id='Vu2Sd'></legend><bdo id='Vu2Sd'><pre id='Vu2Sd'><center id='Vu2Sd'></center></pre></bdo></b><th id='Vu2Sd'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Vu2Sd'><tfoot id='Vu2Sd'></tfoot><dl id='Vu2Sd'><fieldset id='Vu2Sd'></fieldset></dl></div>
                        <bdo id='Vu2Sd'></bdo><ul id='Vu2Sd'></ul>

                        <legend id='Vu2Sd'><style id='Vu2Sd'><dir id='Vu2Sd'><q id='Vu2Sd'></q></dir></style></legend>

                          <tbody id='Vu2Sd'></tbody>

                          1. <tfoot id='Vu2Sd'></tfoot>

                            <small id='Vu2Sd'></small><noframes id='Vu2Sd'>

                            主站蜘蛛池模板: aⅴ色国产 欧美 | 人人做人人澡人人爽欧美 | 久久亚洲国产精品 | 精品毛片 | 中文字幕成人 | 狠狠艹| 91超碰在线 | 国产欧美精品区一区二区三区 | 国产一区二区三区高清 | 97国产精品| 在线看日韩av | 日韩欧美成人一区二区三区 | 日韩欧美中文 | av特级毛片 | 狠狠入ady亚洲精品经典电影 | 国产精久久久久久久妇剪断 | 日韩av一区二区在线观看 | a中文在线视频 | 黄色一级免费观看 | 国产高清在线精品 | 国产高清免费 | 大学生a级毛片免费视频 | 一级黄色片日本 | 亚洲成色777777在线观看影院 | 免费一看一级毛片 | 欧美视频二区 | 久久久久久久久久久国产 | 国产日韩欧美二区 | 久久国色 | 久久精品亚洲 | 成人免费激情视频 | 在线播放国产一区二区三区 | 日本国产精品视频 | 欧美欧美欧美 | 爱爱综合网 | 国产中文 | 亚洲精品视频在线观看视频 | 国产精品爱久久久久久久 | 天天躁天天操 | 99re热这里只有精品视频 | 成人免费看片 |