def new_order_notify(self, kexchange, type, maker_only=True, amount=None, price=None):
order = super().new_order(kexchange, type, maker_only, amount, price)
if order:
# self.notify_msg(order['type'], order['price'])
t = threading.Thread(target = self.notify_msg, args=(order['type'], order['price'],))
t.start()
logging.info("current has %d threads" % (threading.activeCount() - 1))
python类activeCount()的实例源码
def new_order_notify(self, kexchange, type, maker_only=True, amount=None, price=None):
order = super().new_order(kexchange, type, maker_only, amount, price)
if order:
# self.notify_msg(order['type'], order['price'])
t = threading.Thread(target = self.notify_msg, args=(order['type'], order['price'],))
t.start()
logging.info("current has %d threads" % (threading.activeCount() - 1))
def t_join(m_count):
tmp_count = 0
i = 0
while True:
time.sleep(1)
ac_count = threading.activeCount()
if ac_count < m_count and ac_count == tmp_count:
i+=1
else:
i = 0
tmp_count = ac_count
#print ac_count,queue.qsize()
if (queue.empty() and threading.activeCount() <= 1) or i > 5:
break
def t_join(m_count):
tmp_count = 0
i = 0
while True:
time.sleep(2)
ac_count = threading.activeCount()
if ac_count < m_count and ac_count == tmp_count:
i+=1
else:
i = 0
tmp_count = ac_count
#print ac_count,queue.qsize()
if (queue.empty() and threading.activeCount() <= 1) or i > 5:
break
def t_join(m_count):
tmp_count = 0
i = 0
while True:
time.sleep(1)
ac_count = threading.activeCount()
if ac_count < m_count and ac_count == tmp_count:
i+=1
else:
i = 0
tmp_count = ac_count
#print ac_count,queue.qsize()
if (queue.empty() and threading.activeCount() <= 1) or i > 5:
break
def test_old_threading_api(self):
# Just a quick sanity check to make sure the old method names are
# still present
t = threading.Thread()
t.isDaemon()
t.setDaemon(True)
t.getName()
t.setName("name")
t.isAlive()
e = threading.Event()
e.isSet()
threading.activeCount()
def getActiveCount(self):
return threading.activeCount() - 1
def get(self):
""" ???? """
if self.readBytes < self.totalBytes:
shards = range(self.totalBytes)
os.mkdir(self.tempdir)
for i in range(0, self.totalBytes, self.readBytes):
point = shards[i: i + self.readBytes]
headers = { "Range": "bytes=%s-%s" % (point[0], point[-1]) }
filename = os.path.join(self.tempdir, str(point[-1]))
thread = self.threadPool.get()
t = thread(target=self.write_to_file, args=[filename, headers])
t.start()
while activeCount() > 1:
time.sleep(1)
results = sorted(map(int, os.listdir(self.tempdir)))
with open(self.filename, "ab") as wfd:
for result in results:
f = os.path.join(self.tempdir, str(result))
with open(f, "rb") as rfd:
content = True
while content:
content = rfd.read(1024)
wfd.write(content)
self.delete_temp_dir()
else:
self.write_to_file(filename=self.filename, useThread=False)
def num_threads():
'''add information about the number of threads currently running to the
event'''
return threading.activeCount()
# run factorial. libh_builder comes with some fields already populated
# (namely, "version", "num_threads", and "range")
def hack_subprocess():
"""subprocess functions may throw exceptions when used in multiple threads.
See http://bugs.python.org/issue1731717 for more information.
"""
global SUBPROCESS_CLEANUP_HACKED
if not SUBPROCESS_CLEANUP_HACKED and threading.activeCount() != 1:
# Only hack if there is ever multiple threads.
# There is no point to leak with only one thread.
subprocess._cleanup = lambda: None
SUBPROCESS_CLEANUP_HACKED = True
def hack_subprocess():
"""subprocess functions may throw exceptions when used in multiple threads.
See http://bugs.python.org/issue1731717 for more information.
"""
global SUBPROCESS_CLEANUP_HACKED
if not SUBPROCESS_CLEANUP_HACKED and threading.activeCount() != 1:
# Only hack if there is ever multiple threads.
# There is no point to leak with only one thread.
subprocess._cleanup = lambda: None
SUBPROCESS_CLEANUP_HACKED = True
def _map_len(self):
return threading.activeCount()
def run(self):#????????????
time.sleep(5)
print "current has %d threads\r" % (threading.activeCount() - 1)
print 'the arg thread is:%s\r' % self.arg
def run(self):#????????????
time.sleep(5)
print 'the arg thread is:%s\r' % self.arg
print "current has %d threads" % (threading.activeCount() - 1)
def __init__(self, jars=[], jvm_started=False, mark_time_ranges=False, include_range=False):
"""Initializes SUTime.
"""
self.mark_time_ranges = mark_time_ranges
self.include_range = include_range
self.jars = jars
self._is_loaded = False
self._lock = threading.Lock()
if not jvm_started:
self._classpath = self._create_classpath()
self._start_jvm()
try:
# make it thread-safe
if threading.activeCount() > 1:
if jpype.isThreadAttachedToJVM() is not 1:
jpype.attachThreadToJVM()
self._lock.acquire()
SUTimeWrapper = jpype.JClass(
'edu.stanford.nlp.python.SUTimeWrapper')
self._sutime = SUTimeWrapper(
self.mark_time_ranges, self.include_range)
self._is_loaded = True
finally:
self._lock.release()
def _load_dns_servers(self):
print '[+] Initializing, validate DNS servers ...'
self.dns_servers = []
# f=open('./dict/dns_servers.txt','a')
with open('./dns_servers.txt') as f:
# f=['114.114.114.114','114.114.115.115','180.76.76.76','223.5.5.5','223.6.6.6']
for line in f:
server = line.strip()
if not server:
continue
while True:
if threading.activeCount() < 50:
t = threading.Thread(target=self._test_server, args=(server,))
t.start()
break
else:
time.sleep(0.1)
while threading.activeCount() > 2:
time.sleep(0.1)
self.dns_count = len(self.dns_servers)
sys.stdout.write('\n')
print '[+] Found %s available DNS Servers in total' % self.dns_count
if self.dns_count == 0:
print '[ERROR] No DNS Servers available.'
self.STOP_ME = True
sys.exit(-1)
def _load_dns_servers(self):
print('[+] Initializing, validate DNS servers ...')
self.dns_servers = []
thread_list = []
# with open('dict/dns_servers.txt') as f:
with open('api/dict/dns_servers.txt') as f:
for line in f:
server = line.strip()
if not server:
continue
while True:
if threading.activeCount() < 50:
t = threading.Thread(target=self._test_server, args=(server,))
t.setDaemon(True)
t.start()
thread_list.append(t)
break
else:
time.sleep(0.1)
while True:
flag_finished = True
for i in thread_list:
if i.isAlive():
flag_finished = False
if flag_finished:
break
time.sleep(0.1)
self.dns_count = len(self.dns_servers)
sys.stdout.write('\n')
print('[+] Found %s available DNS servers in total' % self.dns_count)
def test_old_threading_api(self):
# Just a quick sanity check to make sure the old method names are
# still present
t = threading.Thread()
t.isDaemon()
t.setDaemon(True)
t.getName()
t.setName("name")
t.isAlive()
e = threading.Event()
e.isSet()
threading.activeCount()
def tearDown(self):
self.console.working = False
self.console.wait_for_threads()
sys.stdout.write("\tactive threads count : %s " % threading.activeCount())
# sys.stderr.write("%s\n" % threading.enumerate())
def run(self):
"""Runs the queue, goes through all of the actions and their recursive definitions."""
threads = []
for i in range(self.num_threads):
threads.append(_Action_Queue_Thread(name = '_Action_Queue_Thread' + str(i), action_queue = self))
for thread in threads:
thread.start()
print(threading.activeCount())
for thread in threads:
thread.join()