twblue/src/mysc/thread_utils.py

34 lines
945 B
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-01-19 19:15:57 +01:00
import logging
log = logging.getLogger("mysc.thread_utils")
import threading
import wx
2015-01-08 15:01:45 +01:00
from pubsub import pub
from twython import TwythonRateLimitError
import time
def call_threaded(func, *args, **kwargs):
#Call the given function in a daemonized thread and return the thread.
def new_func(*a, **k):
try:
func(*a, **k)
except TwythonRateLimitError:
2015-01-13 19:31:37 +01:00
pass
2015-01-19 00:19:39 +01:00
except:
2015-01-19 19:15:57 +01:00
log.exception("Thread %d with function %r, args of %r, and kwargs of %r failed to run." % (threading.current_thread().ident, func, a, k))
# pass
thread = threading.Thread(target=new_func, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread
2015-01-08 15:01:45 +01:00
def stream_threaded(func, *args, **kwargs):
def new_func(*a, **k):
2015-05-03 00:22:28 +02:00
# try:
func(**k)
# except:
# pub.sendMessage("streamError", session=a[0])
2015-01-08 15:01:45 +01:00
thread = threading.Thread(target=new_func, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread