python - 我想用 self 参数线程化一个函数
问题描述
我想线程化这个函数,但不知道当 self 参数到位时如何线程化。任何人都知道我该怎么做。我将不胜感激
这是功能
def processinformation(self):
app = App.get_running_app()
session = requests.Session()
self.notif_stream = session.get("**********************************" + app.displayname + "/.json", stream=True)
for line in self.notif_stream.iter_lines():
if line:
print(json.loads(line))
newline = ast.literal_eval(line.decode('utf-8'))
for key, thevalue in newline.items():
for key, value in thevalue.items():
self.notif = session.get("**********************************" + app.displayname + "/" + key + "/" + "notification" + "/.json")
self.notificationslist.adapter.data.extend([value])
解决方案
好的,我通常没有太多理由编写多线程 Python 程序,但这似乎可行:
#!/usr/bin/env python3
import threading
class MyTarget:
def mymethod(self, arg1, arg2):
print(f"MyTarget, {arg1} {arg2}")
if __name__ == '__main__':
my_target = MyTarget()
t = threading.Thread(target=my_target.mymethod, args=("X", "Y"))
t.start()
# NOTE: In any _real_ program, the main thread would do
# something else, concurrently with the new thread.
t.join()
推荐阅读
- javascript - 如何在 swiper.js 中更改滑动方向?
- mediawiki - 如何编辑此页面?似乎是可视化编辑器的问题?
- mysql - 使用 MySQL 在 EXISTS 条件下更改 TABLE
- amazon-web-services - 从其他账户登录 Cognito 后访问 S3 存储桶
- cluster-analysis - sklearn python 亲和传播 - 有没有一种方法来计算集群中的错误?
- python - Python,浮点记录文本文件,加载浮点数并平均它们
- javascript - 如何翻译用 javascript 生成的文本
- c# - Visual Studio 调试旧版本的代码 - 仅用于实例化类
- javascript - Google Map API 密钥问题 Javascript
- java - 如何限制在 Firestore 中添加和删除侦听器的数量?