大家晚上好。我正在使用Python2.7和线程模块编写多线程程序。下面是一个代码示例:
# Importing myFunc function which will be run in the new thread
from src.functions import myFunc
# Importing a threading module
import threading
# Creating and running new thread initiated by our function
# and some parameters.
t = threading.Thread(target=myFunc, args=(1, 2, 3))
t.start()我知道在C++ (POSIX线程库)中有一个分离()函数,它将运行中的线程置于分离状态。它保证在函数结束后,这个线程会将资源释放回系统。那么,在Python中有类似的函数吗?或者,可能根本没有必要用Python分离线程,线程占用的资源将在线程函数结束后自动释放?
我试图在docs.python.org和谷歌上搜索信息,但没有结果。
发布于 2013-01-06 06:56:55
大家早上好。实际上,答案是‘根本没有必要分离Python中的线程’。这个答案是那里的GaiveR同志给我的。您所需要的只是查看Python (thread_pthread.h)的源代码:
long
PyThread_start_new_thread(void (*func)(void *), void *arg)
{
...
status = pthread_create(&th,
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
&attrs,
#else
(pthread_attr_t*)NULL,
#endif
(void* (*)(void *))func,
(void *)arg
);
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
pthread_attr_destroy(&attrs);
#endif
if (status != 0)
return -1;
pthread_detach(th);
...
}https://stackoverflow.com/questions/14175016
复制相似问题