gpt4 book ai didi

python - 如何杀死正在进行的请求请求对象

转载 作者:行者123 更新时间:2023-11-28 18:52:59 28 4
gpt4 key购买 nike

编辑:在此修订/更新之前,这个问题的主要部分是如何终止 QThread。这已经解决,问题正在修改为如何杀死正在进行的请求剩余对象。

http://docs.python-requests.org/en/v0.10.4/user/advanced/#asynchronous-requests它似乎使用异步请求仍然阻塞 - 用户无法在进行中取消帖子。

基本上这是需要的功能:当用户按下 Stop Uploading 时,上传必须立即停止,我可以使用 stop() 停止线程,但是它只会在循环再次循环后检查是否应该停止。

所以基本上,应该可以使用异步请求,让我检查是否应该在请求期间取消它,但是,我不知道如何。

有什么建议吗?帖子的前一部分仍然相关,所以在下面。
请注意,如何终止 QThread 的最初问题已经解决,因此下面的代码不是太重要,它只是为了上下文,我现在需要帮助的是我刚才描述的内容。




I've been writing a program, it's a photo uploader, I've made a thread in which I upload the files. I can't figure out how to exit the thread. I've tried suggestions i've read from here:

1) I've tried a bool flag, wrapping it both around the method and the for statement that does the work.

2) I've use a 'with' and then tried to set an exception.

I want to be able to cancel uploading, preferably quickly. I've read a lot that it's always recommended to "clean up" the thread before terminating it, I honestly don't know what 'clean it up' means. But, I think I should be able to just kill the thread - since all it's doing is sending the binary data of an image to the TUMBLR api. It shouldn't matter if the request is cancelled early, because It will just cancel the upload in the api too.

Anyway, here is my thread:

class WorkThread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
global w
w = WorkThread
def __del__(slf):
self.wait()
def run(self):
url = 'https://www.tumblr.com/api/write'
files = os.listdir(directory)
for file in files:
file_path = os.path.join(directory + '\\' + file)
file_path = str(file_path)
if file_path[-3:] in ['bmp', 'gif', 'jpg', 'png', 'thm', 'tif', 'yuv']:
self.emit(QtCore.SIGNAL('update(QString)'), "Uploading %s ..." % file_path)
print smart_str(password)
data = {'email': email, 'password': password, 'type': 'photo'}
with open(file_path, 'rb') as photo:
r = requests.post(url, data=data, files={'data': photo})
print r.content
self.emit(QtCore.SIGNAL('update(QString)'), 'All finished! Go check out your stuff on tumblr :)\n')
return

This is how im calling it.

def upload(self):
self.doneBox.clear()

self.workThread = WorkThread()
self.connect( self.workThread, QtCore.SIGNAL("update(QString)"), self.startUploading )
self.workThread.start()

Can anyone suggest in a way that I can terminate the thread quickly and quietly? OR if you think that's not good, a way to stop it safely.

HOWEVER, If I do not kill it instantly, and it goes through the for loop again in the run() method, It will upload the photo that it was uploading WHEN the user presses "Stop Uploading". I wouldn't want it to do that, I'd prefer to have it stop uploading the current photo the second the user presses "Stop Uploading".

Thanks.

最佳答案

我不确定你在用 global w; 做什么w = WorkThread 但它似乎毫无意义,因为你没有对它做任何事情。

在你的 __init__() 你想要一个标志:

def __init__(self):
...
self._isRunning = False

在您的 run() 方法中,您只需检查此标志并在需要时退出:

def run(self):
self._isRunning = True
while True:
if not self._isRunning:
# clean up or close anything here
return
# do rest of work

也许可以添加一个简单的便捷方法:

def stop(self):
self._isRunning = False
self.wait()

当文档提到清理时,他们谈论的是关闭您可能已经打开的资源、撤消可能部分启动的事情,或者您可能希望在刚刚开始之前发出信号或处理的任何其他事情杀死线程对象。对于您的上传者,您想在开始每个上传过程之前检查线程是否应该退出。这将使您有机会停止线程并让它在继续进行另一项工作之前退出。

关于python - 如何杀死正在进行的请求请求对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9389414/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com