Python 我想用self参数线程化一个函数
我想线程这个函数,但不知道如何线程时,自我参数到位。任何人都知道我该怎么做。我将不胜感激 下面是函数Python 我想用self参数线程化一个函数,python,multithreading,python-multithreading,Python,Multithreading,Python Multithreading,我想线程这个函数,但不知道如何线程时,自我参数到位。任何人都知道我该怎么做。我将不胜感激 下面是函数 def processinformation(self): app = App.get_running_app() session = requests.Session() self.notif_stream = session.get("**********************************" + app.displayname + "/.json", s
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()
我不知道是否有更简洁的方法,但如果你知道如何编写一个顶级函数来调用给定对象上的实例方法,并且知道如何创建一个新线程来调用你的顶级函数,那么问题就解决了,对吗?@SolomonSlowexample@TwistStack,编辑了我的解决方案,将您的“变通方法”融入其中。这有助于其他可能认为此答案有用的人。