TheadPoolExecutor with a bounded queue in Python

For tasks that are I/O bound, such as network calls, parallelizing across multiple threads can greatly increase throughput. Python 3 comes with a ThreadPoolExecutor class that makes it easy to queue up units of work that will by run by a pool of threads. ThreadPoolExecutor has an unbounded queue though, and sometimes I want to submit only a certain number of tasks before blocking on the submit() method. For example, sending a large number of requests to a server as fast as possible without buffering all the requests in memory. The below BoundedExecutor class below accomplishes this. Note that it delegates to a ThreadPoolExecutor instance instead of subclassing ThreadPoolExecutor, this decouples it from the underlying implementation of ThreadPoolExecutor (see Effective Java [affiliate link] Item #16: Favor composition over inheritance).

Continue reading “TheadPoolExecutor with a bounded queue in Python”

How to Avoid Static Helper Classes

Static helper classes are classes which contain only static methods, often having names that end in Helper or Utils. The existence of such a class usually indicates a sub-optimal design. In this post I’ll talk about why these classes are a poor design choice in an object oriented language such as Java and how to refactor to a better design.

How to avoid static helpers…

Simplify Code by Encapsulating Collections

A quick survey: Below are two methods that return school district ratings given a list of houses. Which method do you find more to the point?

Method A
public List<Rating> getDistrictRatings(List<House> houses, Price maxPrice) {
  Set<SchoolDistrict> districts =
      .filter(house -> house.price().isLessThan(maxPrice))
      .map(house -> house.getSchoolDistrict())

  return ratingService.rateDistricts(districts);
Method B
public List<Rating> getDistrictRatings(Houses houses, Price maxPrice) {
  Set<SchoolDistrict>=> districts = houses.below(maxPrice)
  return ratingService.rateDistricts(districts);

How to achieve the cleaner method…

How to avoid Object types in Java using the Adapter Pattern

Using the Object type for a method parameter or instance variable in Java application code is a choice that will later haunt you. The temptation to use Object strikes when common functionality needs to be extracted from otherwise unrelated classes and it is impossible for these classes to share a common interface. Seeing this problem can indicate that a larger refactoring is in order, however for now we’ll focus on this specific issue.

How to avoid using Object…