• Latest
  • Trending
ScheduledThreadPoolExecutor in Python

ScheduledThreadPoolExecutor in Python

July 5, 2022
Bolt Opens Africa Hub in Nairobi, Kenya

Bolt Opens Africa Hub in Nairobi, Kenya

July 6, 2022
Google Translate adds 10 new African languages

Google Translate adds 10 new African languages

July 6, 2022
Ericsson invites participants to its ‘Together Apart Hackathon’ in Nigeria

Ericsson invites participants to its ‘Together Apart Hackathon’ in Nigeria

July 6, 2022
Ghana GIPC And OT Sign MoU with America’s led black-owned tech business

Ghana GIPC And OT Sign MoU with America’s led black-owned tech business

July 6, 2022
Nigeria, Mozambique approve SpaceX Internet Deployment

Nigeria, Mozambique approve SpaceX Internet Deployment

July 6, 2022
IXAfrica to Build Africa’s Largest Hyperscale-Ready Datacentre in Nairobi

IXAfrica to Build Africa’s Largest Hyperscale-Ready Datacentre in Nairobi

July 6, 2022
Uganda to Launch its First Low Earth Orbit Satellite in September

Uganda to Launch its First Low Earth Orbit Satellite in September

July 6, 2022
Africa Data Centres to Build second 20MW Data Centre in Cape Town

Africa Data Centres to Build second 20MW Data Centre in Cape Town

July 6, 2022
Kingston Announces FURY Beast RGB DDR5 Memory

Kingston Announces FURY Beast RGB DDR5 Memory

July 6, 2022
Akasa Launches the DuoDock MX Dual NVMe Dock

Akasa Launches the DuoDock MX Dual NVMe Dock

July 6, 2022
Areca ARC-1686 Entry-Level NVMe Hardware RAID Adapters

Areca ARC-1686 Entry-Level NVMe Hardware RAID Adapters

July 6, 2022
TeamGroup Xtreem DDR4-4000 CL15 2x 16 GB

TeamGroup Xtreem DDR4-4000 CL15 2x 16 GB

July 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Wednesday, 6 July, 2022
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

ScheduledThreadPoolExecutor in Python

by ITECHNEWS
July 5, 2022
in Data Science, Leading Stories
0 0
0
ScheduledThreadPoolExecutor in Python

Similar to Java APIs provided in ScheduledThreadPoolExecutor, I’ve implemented the same in Python. Read further for a detailed explanation.

Thread Pool

A thread pool is a design pattern in which multiple threads are maintained to perform the tasks submitted to it. Thread pools are used where multiple short-lived tasks need to be executed and also to increase the performance of the application.

Benefits of using a thread pool

Instead of maintaining a thread pool, we can also create a thread for each task as needed. The main benefit of using a thread pool is that we avoid the cost of creating and destroying the threads. From Wikipedia,

  1. Creating too many threads wastes resources and costs time creating the unused threads.
  2. Destroying too many threads requires more time later when creating them again.
  3. Creating threads too slowly might result in poor client performance (long wait times).
  4. Destroying threads too slowly may starve other processes of resources.

ThreadPoolExecutor

A ThreadPoolExecutor is a kind of supervisor program using which the tasks are submitted to the thread pool. Each programming language has some kind of concurrency package where the thread pool executors are provided. For example, concurrency is provided by the java.util.concurrent package, whereas in Python, it’s provided by the concurrent.futures package.

When a task is submitted to a thread pool executor, a thread is assigned to execute the task and return the result. The number of threads in a thread pool is decided by the computational resource available in a computer.

ScheduledThreadPoolExecutor

ScheduledThreadPoolExecutor is an executor service where the submitted tasks are executed with an initial delay and a periodic interval repeatedly.

ScheduledThreadPoolExecutor comes in handy when we need to run a particular task repeatedly at a specific interval. Example: Checking the replication of a component every 10 minutes and triggering a mail if there is slowness or failure.

The implementation of a ScheduledThreadPoolExecutor is simply done by extending the ThreadPoolExecutor and by maintaining a delay queue to provide the tasks based on the interval. Most languages provide the ScheduledThreadPoolExecutor as a part of their concurrency package. Java provides the ScheduledThreadPoolExecutor in its concurrency package by default. Java provides two methods to execute the task periodically.

  1. scheduleAtFixedRate — Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.
  2. scheduleWithFixedDelay — Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.

ScheduledThreadPoolExecutor in Python

Python provides two different modules, sched and concurrent.futures and it is in the hands of the user to implement the ScheduledThreadPoolExecutor. Though there are packages that provide scheduling in Python, there is no implementation as close to the one provided by Java. So, I’ve implemented the same with the APIs to closely resemble the ones in Java.

The Python package for ScheduledThreadPoolExecutor is published in PyPIas well. You can install it using pip install scheduled-thread-pool-executor . It provides the three main methods schedule, schedule_at_fixed_rate, schedule_at_fixed_delay similar to the one in Java. I believe this will be useful for many who are in search of such implementations in the future.

from scheduled_thread_pool_executor import ScheduledThreadPoolExecutor
scheduled_executor = ScheduledThreadPoolExecutor(max_workers=5)
scheduled_executor.schedule(task, 0) # equals to schedule once, where task is a callable
scheduled_executor.schedule_at_fixed_rate(task, 0, 5) # schedule immediately and run periodically for every 5 secs
scheduled_executor.schedule_at_fixed_delay(task, 5, 10) # schedule after 5secs (initial delay) and run periodically for every 10secs
view rawscheduled_thread_pool_executor.py hosted with ❤ by GitHub

 

GitHub logo syogaraj / scheduled_thread_pool_executor

Scheduled Thread Pool Executor implementation in python

Project generated with PyScaffold PyPI-Server

Scheduled Thread Pool Executor

Scheduled Thread Pool Executor implementation in python

Makes use of delayed queue implementation to submit tasks to the thread pool.

Usage

from scheduled_thread_pool_executor import ScheduledThreadPoolExecutor
scheduled_executor = ScheduledThreadPoolExecutor(max_workers=5)
scheduled_executor.schedule(task, 0)  # equals to schedule once, where task is a callable
scheduled_executor.schedule_at_fixed_rate(task, 0, 5)  # schedule immediately and run periodically for every 5 secs
scheduled_executor.schedule_at_fixed_delay(task, 5, 10)  # schedule after 5secs (initial delay) and run periodically for every 10secs

Note

This project has been set up using PyScaffold 4.1.1. For details and usage information on PyScaffold see https://pyscaffold.org/.

View on GitHub

YOU MAY ALSO LIKE

Bolt Opens Africa Hub in Nairobi, Kenya

Google Translate adds 10 new African languages

Similar to Java APIs provided in ScheduledThreadPoolExecutor, I’ve implemented the same in Python. Read further for a detailed explanation.

Thread Pool

A thread pool is a design pattern in which multiple threads are maintained to perform the tasks submitted to it. Thread pools are used where multiple short-lived tasks need to be executed and also to increase the performance of the application.

Benefits of using a thread pool

Instead of maintaining a thread pool, we can also create a thread for each task as needed. The main benefit of using a thread pool is that we avoid the cost of creating and destroying the threads. From Wikipedia,

  1. Creating too many threads wastes resources and costs time creating the unused threads.
  2. Destroying too many threads requires more time later when creating them again.
  3. Creating threads too slowly might result in poor client performance (long wait times).
  4. Destroying threads too slowly may starve other processes of resources.

ThreadPoolExecutor

A ThreadPoolExecutor is a kind of supervisor program using which the tasks are submitted to the thread pool. Each programming language has some kind of concurrency package where the thread pool executors are provided. For example, concurrency is provided by the java.util.concurrent package, whereas in Python, it’s provided by the concurrent.futures package.

When a task is submitted to a thread pool executor, a thread is assigned to execute the task and return the result. The number of threads in a thread pool is decided by the computational resource available in a computer.

ScheduledThreadPoolExecutor

ScheduledThreadPoolExecutor is an executor service where the submitted tasks are executed with an initial delay and a periodic interval repeatedly.

ScheduledThreadPoolExecutor comes in handy when we need to run a particular task repeatedly at a specific interval. Example: Checking the replication of a component every 10 minutes and triggering a mail if there is slowness or failure.

The implementation of a ScheduledThreadPoolExecutor is simply done by extending the ThreadPoolExecutor and by maintaining a delay queue to provide the tasks based on the interval. Most languages provide the ScheduledThreadPoolExecutor as a part of their concurrency package. Java provides the ScheduledThreadPoolExecutor in its concurrency package by default. Java provides two methods to execute the task periodically.

  1. scheduleAtFixedRate — Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.
  2. scheduleWithFixedDelay — Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.

ScheduledThreadPoolExecutor in Python

Python provides two different modules, sched and concurrent.futures and it is in the hands of the user to implement the ScheduledThreadPoolExecutor. Though there are packages that provide scheduling in Python, there is no implementation as close to the one provided by Java. So, I’ve implemented the same with the APIs to closely resemble the ones in Java.

The Python package for ScheduledThreadPoolExecutor is published in PyPIas well. You can install it using pip install scheduled-thread-pool-executor . It provides the three main methods schedule, schedule_at_fixed_rate, schedule_at_fixed_delay similar to the one in Java. I believe this will be useful for many who are in search of such implementations in the future.

from scheduled_thread_pool_executor import ScheduledThreadPoolExecutor
scheduled_executor = ScheduledThreadPoolExecutor(max_workers=5)
scheduled_executor.schedule(task, 0) # equals to schedule once, where task is a callable
scheduled_executor.schedule_at_fixed_rate(task, 0, 5) # schedule immediately and run periodically for every 5 secs
scheduled_executor.schedule_at_fixed_delay(task, 5, 10) # schedule after 5secs (initial delay) and run periodically for every 10secs
view rawscheduled_thread_pool_executor.py hosted with ❤ by GitHub

 

GitHub logo syogaraj / scheduled_thread_pool_executor

Scheduled Thread Pool Executor implementation in python

Project generated with PyScaffold PyPI-Server

Scheduled Thread Pool Executor

Scheduled Thread Pool Executor implementation in python

Makes use of delayed queue implementation to submit tasks to the thread pool.

Usage

from scheduled_thread_pool_executor import ScheduledThreadPoolExecutor
scheduled_executor = ScheduledThreadPoolExecutor(max_workers=5)
scheduled_executor.schedule(task, 0)  # equals to schedule once, where task is a callable
scheduled_executor.schedule_at_fixed_rate(task, 0, 5)  # schedule immediately and run periodically for every 5 secs
scheduled_executor.schedule_at_fixed_delay(task, 5, 10)  # schedule after 5secs (initial delay) and run periodically for every 10secs

Note

This project has been set up using PyScaffold 4.1.1. For details and usage information on PyScaffold see https://pyscaffold.org/.

View on GitHub
Source: Yogaraj.S
Tags: ScheduledThreadPoolExecutor in Python
ShareTweetShare
Plugin Install : Subscribe Push Notification need OneSignal plugin to be installed.

Search

No Result
View All Result

Recent News

Bolt Opens Africa Hub in Nairobi, Kenya

Bolt Opens Africa Hub in Nairobi, Kenya

July 6, 2022
Google Translate adds 10 new African languages

Google Translate adds 10 new African languages

July 6, 2022
Ericsson invites participants to its ‘Together Apart Hackathon’ in Nigeria

Ericsson invites participants to its ‘Together Apart Hackathon’ in Nigeria

July 6, 2022

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Bolt Opens Africa Hub in Nairobi, Kenya

Bolt Opens Africa Hub in Nairobi, Kenya

July 6, 2022
Google Translate adds 10 new African languages

Google Translate adds 10 new African languages

July 6, 2022

Recent News

  • Bolt Opens Africa Hub in Nairobi, Kenya July 6, 2022
  • Google Translate adds 10 new African languages July 6, 2022
  • Ericsson invites participants to its ‘Together Apart Hackathon’ in Nigeria July 6, 2022
  • Ghana GIPC And OT Sign MoU with America’s led black-owned tech business July 6, 2022
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© 2021 iTechNewsOnline.Com - Powered by BackUpDataSystems

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© 2021 iTechNewsOnline.Com - Powered by BackUpDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Go to mobile version