-
Notifications
You must be signed in to change notification settings - Fork 1
/
emit.py
130 lines (100 loc) · 2.97 KB
/
emit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Python example demonstrating when callbacks are run in a threaded environment
# John Stowers
import os
import sys
import time
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GObject
GObject.threads_init()
import threading
import thread
import time
import random
import StringIO
import re
import ConfigParser
from signal import signal, SIGWINCH, SIGKILL, SIGTERM
from IPython.core.debugger import Tracer
from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(mode='Verbose',
color_scheme='Linux',
call_pdb=True,
ostream=sys.__stdout__)
from colorlog import ColoredFormatter
import logging
from gettext import gettext as _
import traceback
from functools import wraps
import Queue
def setup_logger():
"""Return a logger with a default ColoredFormatter."""
formatter = ColoredFormatter(
"(%(threadName)-9s) %(log_color)s%(levelname)-8s%(reset)s %(message_log_color)s%(message)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
},
secondary_log_colors={
'message': {
'ERROR': 'red',
'CRITICAL': 'red',
'DEBUG': 'yellow'
}
},
style='%'
)
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
def trace(func):
"""Tracing wrapper to log when function enter/exit happens.
:param func: Function to wrap
:type func: callable
"""
@wraps(func)
def wrapper(*args, **kwargs):
logger.debug('Start {!r}'. format(func.__name__))
result = func(*args, **kwargs)
logger.debug('End {!r}'. format(func.__name__))
return result
return wrapper
# Create a player
logger = setup_logger()
class Test(threading.Thread, GObject.GObject):
__gsignals__ = {
"now": (GObject.SignalFlags.RUN_FIRST, None, (str, str)),
"idle": (GObject.SignalFlags.RUN_FIRST, None, (str, str))
}
def __init__(self):
threading.Thread.__init__(self)
GObject.GObject.__init__(self)
def run(self):
print "run"
while 1:
self.emit("now", "now", str(thread.get_ident()))
GObject.idle_add(self.emit, "idle", "idle",
str(thread.get_ident()))
time.sleep(1)
def cb(sender, how, i):
print "%s cb %s vs thread %s" % (how, thread.get_ident(), i)
def tick():
print "ml %s ..." % thread.get_ident()
return True
GObject.timeout_add_seconds(1, tick)
t = Test()
t.connect("now", cb)
t.connect("idle", cb)
t.start()
Gtk.main()