[D]我想在python跑多线程之前查看和修改默认线程的栈大小是调用什么方法

bullswu 2012-05-06 06:50:57
之前在网上我看到了一些资料,但是我还是get不到python的当前线程栈大小.请问有什么方法可以查找和设置栈的大小?

想突破线程的瓶颈,所以我想减小栈的大小,还有能否把堆的空间也用上,谢谢了.
-------------------------------
Double行动:
原帖分数:60
帖子加分:60
...全文
252 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bugs2k 2012-05-07
  • 打赏
  • 举报
回复
class ThreadTests(BaseTestCase):

# Create a bunch of threads, let each do some work, wait until all are
# done.
def test_various_ops(self):
# This takes about n/3 seconds to run (about n/3 clumps of tasks,
# times about 1 second per clump).
NUMTASKS = 10

# no more than 3 of the 10 can run at once
sema = threading.BoundedSemaphore(value=3)
mutex = threading.RLock()
numrunning = Counter()

threads = []

for i in range(NUMTASKS):
t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
threads.append(t)
self.assertEqual(t.ident, None)
self.assertTrue(re.match('<TestThread\(.*, initial\)>', repr(t)))
t.start()

if verbose:
print 'waiting for all tasks to complete'
for t in threads:
t.join(NUMTASKS)
self.assertTrue(not t.is_alive())
self.assertNotEqual(t.ident, 0)
self.assertFalse(t.ident is None)
self.assertTrue(re.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t)))
if verbose:
print 'all tasks done'
self.assertEqual(numrunning.get(), 0)

def test_ident_of_no_threading_threads(self):
# The ident still must work for the main thread and dummy threads.
self.assertFalse(threading.currentThread().ident is None)
def f():
ident.append(threading.currentThread().ident)
done.set()
done = threading.Event()
ident = []
thread.start_new_thread(f, ())
done.wait()
self.assertFalse(ident[0] is None)
# Kill the "immortal" _DummyThread
del threading._active[ident[0]]

# run with a small(ish) thread stack size (256kB)
def test_various_ops_small_stack(self):
if verbose:
print 'with 256kB thread stack size...'
try:
threading.stack_size(262144)
except thread.error:
if verbose:
print 'platform does not support changing thread stack size'
return
self.test_various_ops()
threading.stack_size(0)

# run with a large thread stack size (1MB)
def test_various_ops_large_stack(self):
if verbose:
print 'with 1MB thread stack size...'
try:
threading.stack_size(0x100000)
except thread.error:
if verbose:
print 'platform does not support changing thread stack size'
return
self.test_various_ops()
threading.stack_size(0)
bullswu 2012-05-07
  • 打赏
  • 举报
回复
以上那个函数我也看了,具体是如何使用

在我执行线程之前执行这个函数就可以了?
threading.stack_size(32,768 )
bugs2k 2012-05-07
  • 打赏
  • 举报
回复
32kB is currently the minimum supported stack size value to guarantee sufficient stack space for the interpreter itself
bullswu 2012-05-07
  • 打赏
  • 举报
回复
我按照这个方法设置,在执行线程前设置threading.stack_size(32*1024),然后在我的线程里面while做了循环之后第一次打印是32k的线程,但是第二次确是0,后面几次都这样..

虽然我原来的方法其实通过了别的方式加了线程数,不过还是想搞清楚:
python的线程的堆栈是不是最低只能32*10240?同时也是默认的栈值是32k么?

还有为什么按照你那样使用后第二次循环了却打印0,这是默认的意思还是别的意思.?
bugs2k 2012-05-06
  • 打赏
  • 举报
回复
threading.stack_size([size])
Return the thread stack size used when creating new threads. The optional size argument specifies the stack size to be used for subsequently created threads, and must be 0 (use platform or configured default) or a positive integer value of at least 32,768 (32kB). If changing the thread stack size is unsupported, a ThreadError is raised. If the specified stack size is invalid, a ValueError is raised and the stack size is unmodified. 32kB is currently the minimum supported stack size value to guarantee sufficient stack space for the interpreter itself. Note that some platforms may have particular restrictions on values for the stack size, such as requiring a minimum stack size > 32kB or requiring allocation in multiples of the system memory page size - platform documentation should be referred to for more information (4kB pages are common; using multiples of 4096 for the stack size is the suggested approach in the absence of more specific information). Availability: Windows, systems with POSIX threads.

New in version 2.5.
物联网通信协议测试是保障各类设备间实现可靠数据交互的核心环节。在众多适用于物联网的通信协议中,MQTT(消息队列遥测传输)以其设计简洁与低能耗的优势,获得了广泛应用。为确保MQTT客户端与服务端的实现严格遵循既定标准,并具备良好的互操作性,实施系统化的测试验证至关重要。 为此,采用TTCN-3(树表结合表示法第3版)这一国际标准化测试语言构建的自动化测试框架被引入。该语言擅长表达复杂的测试逻辑与数据结构,同时保持了代码的清晰度与可维护性。基于此框架开发的MQTT协议一致性验证套件,旨在自动化地检验MQTT实现是否完全符合协议规范,并验证其与Eclipse基金会及欧洲电信标准化协会(ETSI)所发布的相关标准的兼容性。这两个组织在物联网通信领域具有广泛影响力,其标准常被视为行业重要参考。 MQTT协议本身存在多个迭代版本,例如3.1、3.1.1以及功能更为丰富的5.0版。一套完备的测试工具必须能够覆盖对这些不同版本的验证,以确保基于各版本开发的设备与应用均能满足一致的质量与可靠性要求,这对于物联网生态的长期稳定运行具有基础性意义。 本资源包内包含核心测试框架文件、一份概述性介绍文档以及一份附加资源文档。这些材料共同提供了关于测试套件功能、应用方法及可能包含的扩展工具或示例的详细信息,旨在协助用户快速理解并部署该测试解决方案。 综上所述,一个基于TTCN-3的高效自动化测试框架,为执行全面、标准的MQTT协议一致性验证提供了理的技术路径。通过此类专业测试套件,开发人员能够有效确保其MQTT实现的规范符合性与系统兼容性,从而为构建稳定、安全的物联网通信环境奠定坚实基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
【负荷预测、电价预测】基于神经网络的负荷预测和价格预测(Matlab代码实现)内容概要:本文档围绕“基于神经网络的负荷预测和电价预测”展开,提供了使用Matlab实现的完整代码与技术方案。内容涵盖电力系统中负荷与电价的时间序列预测模型构建,重点利用神经网络(如BP、LSTM、CNN等)进行数据建模与训练,并结合实际电力数据完成预测任务。文档多次强调该资源适用于科研复现与工程实践,尤其适合需要进行电力系统智能预测的相关研究。文中还提到了多个相关应用场景和技术扩展,体现了较强的综合性与实用性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事能源预测、智能电网相关工作的技术人员;熟悉机器学习或深度学习基本原理者更佳;; 使用场景及目标:①应用于电力系统中的短期/长期负荷与电价预测研究;②支撑学术论文复现、课题项目开发及优化调度系统设计;③帮助理解神经网络在时序预测中的具体实现方式与参数调优策略;; 阅读建议:建议结合文档中提到的其他资源(如网盘链接)下载完整代码进行实操演练,重点关注数据预处理、模型搭建与训练流程,配合Matlab工具箱深入理解算法细节,并尝试迁移至其他预测场景以提升应用能力。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧