Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a simple loop #3

Merged
merged 6 commits into from
Apr 29, 2022
Merged

a simple loop #3

merged 6 commits into from
Apr 29, 2022

Conversation

cuihairu
Copy link
Contributor

@cuihairu cuihairu commented Apr 26, 2022

  1. The kill method is provided to facilitate its own internal calls. Some programs need to support internal shutdown (not the way to accept signals).
# import module
from GracefulKiller import GracefulKiller
    
# create killer
killer = GracefulKiller()

# An rpc program can call the interface to shut down the program
killer.kill()
  1. A default thread loop is provided.

Threaded Loop may not be the best option, but a simple implementation.

# import module
from GracefulKiller import GracefulKiller

# create killer
killer = GracefulKiller()

# create loop object and start loop, wait one second after each loop. 
Loop(killer, 1).start()
  1. singleton
man  signal

# return :

signal() returns the previous value of the signal handler, or SIG_ERR on error.

GracefulKiller objects create multiple, and only one of them takes effect.

  1. shutdown handler
# import module
from GracefulKiller import GracefulKiller, Loop

# Called when graceful shutdown 
def shutdown_handler():
    print("shutdown")
    
# create killer
killer = GracefulKiller(shutdown_handler)

# loop
Loop(killer, 1).start()

# shutdown
killer.kill()

# print
"shutdown"

# Conflicts:
#	GracefulKiller/GracefulKiller.py
#	GracefulKiller/__init__.py
#	GracefulKiller/test_GracefulKiller.py
#	README.md
@MaxMaxoff
Copy link
Owner

@cuihairu, could you please provide some description and use cases for your changes.

@cuihairu
Copy link
Contributor Author

cuihairu commented Apr 28, 2022

@MaxMaxoff my English is not too good, I wish I could express it clearly.

cls.instance = super(Loop, cls).__new__(cls)
return cls.instance

def __init__(self, killer, delay):
Copy link
Owner

@MaxMaxoff MaxMaxoff Apr 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please update killer and delay with default values, like killer=None, delay=1 or etc.

return cls.instance

def __init__(self, killer, delay):
assert killer is not None and isinstance(killer, GracefulKiller)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why it can't be just like this:
assert killer and isinstance(killer, GracefulKiller)?

self.delay = delay

def loop(self):
while True:
Copy link
Owner

@MaxMaxoff MaxMaxoff Apr 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks more simple, what do you think?
while not self.killer.kill_now: time.sleep(self.delay)

@MaxMaxoff
Copy link
Owner

@cuihairu, now I understand what you're offer in this PR.
Could you please update your code according to my comemnts.


def __init__(self, killer, delay):
assert killer is not None and isinstance(killer, GracefulKiller)
if delay is None:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can pass it if you will define default values for delay

@MaxMaxoff MaxMaxoff merged commit aaf46ff into MaxMaxoff:master Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants