I am having a bit of trouble understanding callbacks. I have the following usecase:
From my gui i start a new thread called videoDown (which is its own class). From this thread i want to push the data back to the gui which i have implemented a callback system for. BUT to update the GUI i need to be in the GUI thread and not the thread i currently reside in (which is the videoDown thread).
Code where i start my thread (class Downloader):
def download_single(self, json_data):
form_data = json.loads(json_data)
print self.app
url = form_data["name"]
dt = form_data["dt"] # Download type is audio or video
videoDown = videoDownload(url, dt, self.dd,callback=self.cb,callback_args=("hello", "world",self.app))
videoDown.start()
videoDownload thread:
class videoDownload(threading.Thread):
def __init__(self,url, dt, dd,callback=None, callback_args=None, *args, **kwargs):
threading.Thread.__init__(self)
self.callback = callback
self.url = url
self.dt = dt
self.dd = dd
self.callback_args = callback_args
if self.callback is not None:
self.callback(*self.callback_args)
def run(self):
if self.url.__contains__("https://www.youtube.com/watch?v="):
if self.dt == 'audio':
self._downloadVid(self.url, vid=False)
else:
self._downloadVid(self.url, vid=True)
else:
print "Incorrect url"
def _downloadVid(self, url, vid=False, order_reverse=False, vinName=None):
video = pafy.new(url)
if self.callback is not None: //<--CALLBACK IN HERE
self.callback(*self.callback_args)
streams = video.allstreams
for stream in streams:
print stream
print video
name = u''.join(video.title).encode('utf8')
name = re.sub("[<>:\"/\\|?*]", "", name)
if not vid:
file = video.getbestaudio()
else:
file = video.getbest()
if (order_reverse):
file.download(self.dd + vinName + name + ".mp4", quiet=False, callback=self.mycb)
else:
file.download(self.dd + name + ".mp4", quiet=False, callback=self.mycb)
Callback (also in class Downloader):
def cb(self,param1, param2,param3):
print threading.current_thread()
How exactly do i implement it so i can give data from my video download thread back to the gui thread while the current thread is set to this video download thread.
What do i need to change, i have been struggling with this for hours.
~Greetings