Package: hangpy
HangPyHangPy is a simple background job manager for Python.Its main purpose is to allow scheduling and distribution of asynchronous tasks.It is being developed as a study project for Python. Nevertheless, it is completely functional.RequirementsPython 3.9 (it is not tested on older versions)RedisUsageIn order to use HangPy on your project, it is necessary to install its package using the command below:pipinstallhangpyIt is possible to create a standalone application to use as a server, importing both HangPy and the modules containing the tasks that will be scheduled.
Another possibility is to use HangPy on a thread inside an already existing application.This next code snippet shows a very basic example of the commands necessary in order to start a HangPy server instance:importhangpyimportredis# Instantiating a regular Redis clientredis_client=redis.StrictRedis(host='172.17.0.1',port=6379,password=None)# Instantiating the server and job repositoriesserver_repository=hangpy.RedisServerRepository(redis_client)job_repository=hangpy.RedisJobRepository(redis_client)# Configuring the HangPy server instanceserver_configuration=hangpy.ServerConfigurationDto(slots=10)# Defining the log outputlog_service=hangpy.PrintLogService()# Instantiating the serverserver_service=hangpy.ServerService(server_configuration,server_repository,job_repository,log_service)# Initializing the serverserver_service.start()LogThere is a builtin log class calledPrintLogServicethat can be injected in theServerServiceconstructor. This logger will print the messages on the console. It is possible to build custom loggers inheriting from the abstract classLogService.Stopping the serverIt is possible to use the functionstopavailable on theServerServiceclass.When the HangPy server is signaled to stop, it will wait for the jobs that are running on that instance to end, without fetching any new tasks from the queue.Creating jobsIn order to create a job that can be executed by HangPy, it is necessary to create a class that inherits fromJobActivityBase.It is necessary to override the functionaction, placing the commands to be executed inside.The code snippet below shows the scheduling of a simple job.Suppose that exists a module namedjob_delay, containing the following class:importhangpyimporttime# Creating a job that does nothing but sleep for 10 secondsclassJobDelay(hanpy.JobActivityBase):defaction(self):time.sleep(10)A simple script to schedule the execution of this job would look something like this next snippet:importhangpyimportredisfromjob_delayimportJobDelay# Instantiating a regular Redis clientredis_client=redis.StrictRedis(host='172.17.0.1',port=6379,password=None)# Instantiating the job repositoryjob_repository=hangpy.RedisJobRepository(redis_client)# Scheduling the execution of the jobjob_service=hangpy.JobService(job_repository)job_service.enqueue_job(JobPrintDateTime())ExamplesIn theexamplesfolder there are some scripts that show in a simple way how to use HangPy.The examples include a simple implementation of a standalone HangPy server that runs on the console.ScalabilityHangPy was developed to scale. It is possible to run many instances of servers using the same repositories, to allow the distribution of the jobs processing load.Each server instance can use a different configuration, choosing what best suits the environment.ConfigurationUsing the classServerConfigurationDtois possible to configure the details of each server instance.slots: Set the maximum number of jobs that can be executed in parallel on each server instance.cycle_interval_milliseconds: Sets the time the system sleeps between each processing cycle on the server instance. This sleep time only occurs when a cycle ends and there are no jobs enqueued.Custom RepositoriesHangPy was built in a way to allow that any repository could be used to store its internal data.Although there is builtin support for using Redis, all calls are made through the abstract classesJobRepositoryandServerRepository.For example, if someone wants to use HangPy with its internal data stored in a relational database, it would be enough to implement the methods described on those both interfaces, passing these implementations as arguments to the classServerService.