是否可以在python3中命名ThreadPool的子进程

时间:2017-09-04 13:35:31

标签: multithreading python-3.x python-multiprocessing

在java中,可以设置线程池,以便有一个线程工厂初始化池产生的线程。然后,该工厂可以设置线程,以便它们具有特定的名称或优先级。这是我几年前写的一个例子:

    /** A custom implementation of ThreadFactory which names pool threads accordingly and
     *  allows setting priority of the created threads. Delivers non daemon threads.
     *  
     *  @param threadPriority - desired level or priority for created threads, if this value 
     *  is smaller than Thread.MIN_PRIORITY or greater than Thread.MAX_PRIORITY 
     *  a default value of Thread.NORM_PRIORITY is used. 
     * */
    protected class MyThreadFactory implements ThreadFactory {
        private final ThreadGroup group;
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        private final String namePrefix;
        private final int DEF_PRIORITY = Thread.NORM_PRIORITY;
        private final int priority;

        FeverThreadFactory(int threadPriority) {
            SecurityManager s = System.getSecurityManager();
            group = (s != null)? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
            namePrefix = "worker-pool-thread-";

            boolean validPriority = threadPriority > Thread.MIN_PRIORITY && 
                                    threadPriority < Thread.MAX_PRIORITY;

            this.priority = validPriority ? threadPriority : DEF_PRIORITY;

        }

        public Thread  newThread(Runnable r) {
            Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(),0);

            if (t.isDaemon())
                t.setDaemon(false);

            t.setPriority(this.priority);     
            return t;
        }
    }

我分别从多处理和concurrent.futures模块检查了ThreadPool和ThreadPoolExecutor,但没有看到任何相关内容。是否有可能在Python3中做类似的事情?

0 个答案:

没有答案