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

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

想突破线程的瓶颈,所以我想减小栈的大小,还有能否把堆的空间也用上,谢谢了.
-------------------------------
Double行动:
原帖分数:60
帖子加分:60
...全文
219 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.

37,719

社区成员

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

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