Answers for "thread pool python"

2

how to thread python

import threading, time

def worker():
    """thread worker function"""
    print('Worker')
    return

threads = []
for i in range(5):
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()
    print('Thread')
Posted by: Guest on March-07-2020
0

threadpool in python

import time
from concurrent.futures import ThreadPoolExecutor, Future


def test():
    print("testing this")
    time.sleep(2)
    return "got"

sample = ThreadPoolExecutor(max_workers=2, thread_name_prefix="sample")

for _ in range(10):
  	got: Future = sample.submit(test)
	print(got.result(timeout=3))  # waits for the result (like the join)  # raise TimeoutError  (doesn't quit thread tho)
    # 2 threads at a time
    # plus those 2 threads are reused instead of creating new thread every time
    # that's how it's diff. from threading module

sample.shutdown(cancel_futures=True)  # refer: https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor.shutdown
Posted by: Guest on October-10-2021
0

thread pool java

package com.journaldev.threadpool;

public class WorkerThread implements Runnable {
  
    private String command;
    
    public WorkerThread(String s){
        this.command=s;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
        processCommand();
        System.out.println(Thread.currentThread().getName()+" End.");
    }

    private void processCommand() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString(){
        return this.command;
    }
}
Posted by: Guest on November-19-2020
2

how to thread python

import threading, time

def worker():
    """thread worker function"""
    print('Worker')
    return

threads = []
for i in range(5):
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()
    print('Thread')
Posted by: Guest on March-07-2020
0

threadpool in python

import time
from concurrent.futures import ThreadPoolExecutor, Future


def test():
    print("testing this")
    time.sleep(2)
    return "got"

sample = ThreadPoolExecutor(max_workers=2, thread_name_prefix="sample")

for _ in range(10):
  	got: Future = sample.submit(test)
	print(got.result(timeout=3))  # waits for the result (like the join)  # raise TimeoutError  (doesn't quit thread tho)
    # 2 threads at a time
    # plus those 2 threads are reused instead of creating new thread every time
    # that's how it's diff. from threading module

sample.shutdown(cancel_futures=True)  # refer: https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor.shutdown
Posted by: Guest on October-10-2021
0

thread pool java

package com.journaldev.threadpool;

public class WorkerThread implements Runnable {
  
    private String command;
    
    public WorkerThread(String s){
        this.command=s;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
        processCommand();
        System.out.println(Thread.currentThread().getName()+" End.");
    }

    private void processCommand() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString(){
        return this.command;
    }
}
Posted by: Guest on November-19-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language