私のスクリプトは、このように動作します非クラスからクラスのソケットを閉じます:どう
# other part of code
class request(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
try:
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((host), (port))
socket.send(str.encode("test"))
except:
socket.close()
def loop():
for x in range(5):
request(x).start()
# other
# part
# of code
def startall():
# some other code
choice = input("command: ")
if choice == "request":
loop()
elif choice == "stop":
# ?
# some other code
startall()
入力が「停止」の場合は、要求の送信を停止する方法はありますか?これは単なるサンプルであり、私のスクリプトはこのように動作しないことに注意してください。
class request(threading.Thread):
REQUESTS_ALLOWED = True
def run(self):
while request.REQUESTS_ALLOWED:
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
socket.connect((host), (port))
socket.send(str.encode("test"))
except:
pass # Do what you need
finally:
socket.close()
お知らせソケットを閉じるの交代:私はちょうどあなたがfolowsようにクラスを変更することができ、一度にすべての要求を停止する場合は、あなたが私の問題
で試験しました。終了したスレッドにすべて参加しなければならないと言った...どうすればいいですか?すべてのアクティブなリクエスト/スレッドを停止する必要がありますか? – AllExJ
@AllExJ私はそれが助けてくれることを願って答えを広げた。 – Qeek
それは動作します!ありがとう!!! – AllExJ