简体   繁体   English

python-如何强制线程停止

[英]How to force a thread to stop, python

in my Proxy Project sometimes the Server process (given by teacher) ends with an error (a string beginning with 404) so I would like my thread, which had sent the requests, to stop and not to go on. 在我的代理项目中,有时Server进程(由老师给定)以错误(以404开头的字符串)结束,所以我希望发送请求的线程停止并且不继续。 I have tried with "return" or with "sys.exit()" but this seems to block everything and the Proxy stops receiving requests and creating threads. 我已经尝试过使用“ return”或“ sys.exit()”,但这似乎阻止了所有操作,并且代理服务器停止接收请求并创建线程。 Why? 为什么?

from socket import *
from threading import *
import sys
import colors 
import os
import time

def startPrefetch(pagesToPrefetch, mutex):
    for i in pagesToPrefetch:
        print i

def receivePage(conn, addr, request, mutex):
    HOSTSERVER = "127.0.0.1"
    SERVERPORT = 55555
    socketRequestServer = socket(AF_INET, SOCK_STREAM)
    socketRequestServer.connect((HOSTSERVER, SERVERPORT))
    socketRequestServer.send(request)

    finalResponse = ''

    while True:
        partialResponse = socketRequestServer.recv(64)
        if (not partialResponse): break
        finalResponse = finalResponse+partialResponse        

    if (finalResponse[0] == '4'):
        c = colors.colors()
        print c.ERROR + finalResponse + "INTERNAL SERVER ERROR"
        print c.WHITE
        return
        #sys.exit(1)


    socketRequestServer.close()      
    conn.sendall(finalResponse)
    conn.close()

    pagesToPrefetch = []

    sourceToString = finalResponse.split(' ')
    for i in sourceToString:
        if (len(i) != 0):
            if (i[0] == '<'):
                pagesToPrefetch.append(i)

    startPrefetch(pagesToPrefetch, mutex)


if __name__ == '__main__':
    HOST = '127.0.0.1'
    PORT = 55554

    print 'Creating socket'
    socketProxy = socket(AF_INET, SOCK_STREAM)

    print 'bind()'
    socketProxy.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    socketProxy.bind((HOST, PORT))

    #Cache directory and mutex for it
    mutex = Semaphore(1)
    try:
        os.mkdir("cache")
    except OSError:
        print 'Created cache directory'

    while True:
        print 'Waiting for connection request'
        socketProxy.listen(1)
        conn, addr = socketProxy.accept()

        print 'Connected to ', addr

        request = conn.recv(512)        

        receiver = Thread(target = receivePage, args = (conn, addr, request, mutex))
        receiver.start()

Using return is the correct way ( well, you should close connections before doing that, though ). 使用return是正确的方法(不过,您应该在这样做之前关闭连接)。

As David suggested this line: 正如David所建议的那样:

request = conn.recv(512)

is probably the culprit. 可能是罪魁祸首。 If the connection is made but no data is sent then the main thread will be locked. 如果建立连接但未发送数据,则主线程将被锁定。 Not to mention that the request might be bigger then 512. 更不用说该请求可能大于512。

Move that line inside receivePage function ( and don't pass request to Thread constructor ) and let us know whether it works. 将该行receivePage函数中(不要将request传递给Thread构造函数),然后让我们知道它是否有效。

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

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