简体   繁体   English

Python线程阻止进一步执行

[英]Python thread blocking further execution

I have been trying to write a python script that initiates a thread to listen on a socket and send HTTP data to another application to be launched by the same program. 我一直在尝试编写一个python脚本,该脚本启动一个线程以侦听套接字并将HTTP数据发送到要由同一程序启动的另一个应用程序。 There is a requirement for the socket server to be running prior to executing the application. 在执行应用程序之前,要求套接字服务器正在运行。 However, the thread running the socket server blocks further execution of the program and it freezes where it is listening. 但是,运行套接字服务器的线程阻止了程序的进一步执行,并且冻结了正在侦听的位置。 Putting some dummy code. 放置一些伪代码。

In module 1: 在模块1中:

def runServer(Port, Host, q):
    HTTPServerObj = HTTPServer((Host, Port), RequestHandler)
    HTTPServerObj.handle_request()
    HTTPServerObj.server_close()
    q.put((True, {'messageDoNotDuplicate': 'Data sent successfully by the server'}))

class SpoofHTTPServer(object):
    def runServerThread(self):
        q = Queue.Queue()
        serverThread=Thread(target=runServer, args=(self.Port, self.Host, q))
        serverThread.daemon=True
        serverThread.start()
        result = q.get()
        print result
        return result

In module 2: 在模块2中:

from module1 import SpoofHTTPServer

spoofHTTPServer = SpoofHTTPServer()
result = spoofHTTPServer.runServerThread()
rc = myApp.start()

The myApp.start() never gets executed as the thread is blocking it. myApp.start()永远不会执行,因为线程正在阻止它。

It looks to me like the method that blocks execution is not the thread but q.get() . 在我看来,阻止执行的方法不是线程而是q.get() It will listen to the Queue until an item is available, but since it's executed before running the client application nothing ever gets posted into the queue. 它将侦听Queue直到有可用项为止,但是由于它是在运行客户端应用程序之前执行的,因此不会将任何内容张贴到队列中。 Maybe you should return q instead and listen to the queue in module 2 after calling myApp.start() ? 也许您应该改为return q并在调用myApp.start()之后myApp.start()模块2中的队列?

This may work for you from Python 3. Make a connection to ('localhost', 8080) to see it work. 在Python 3中,这可能对您有用。建立与('localhost', 8080)的连接('localhost', 8080)以查看其工作原理。

import queue as Queue
from threading import Thread
from http.server import HTTPServer
from socketserver import BaseRequestHandler as RequestHandler

def runServer(Port, Host, q):
    HTTPServerObj = HTTPServer((Host, Port), RequestHandler)
    HTTPServerObj.handle_request()
    HTTPServerObj.server_close()
    q.put((True, {'messageDoNotDuplicate':
                  'Data sent successfully by the server'}))

class SpoofHTTPServer(object):

    Port = 8080
    Host = ''

    def runServerThread(self):
        q = Queue.Queue()
        serverThread=Thread(target=runServer, args=(self.Port, self.Host, q))
        serverThread.daemon=True
        serverThread.start()
        result = q.get()
        print(result)
        return result

spoofHTTPServer = SpoofHTTPServer()
result = spoofHTTPServer.runServerThread()
##rc = myApp.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM