jaraco / irc

Full-featured Python IRC library for Python.
MIT License
390 stars 85 forks source link

allow exiting process_forever gracefully #108

Closed igel-kun closed 7 years ago

igel-kun commented 7 years ago

hi, I wanted to use an IRC connection as a subroutine in some code I'm writing, but in all the examples I looked at, whenever the task was done, the program exits via sys.exit() which is not an option for me. So I added an active flag to the IRC class that can be set to false to stop process_forever(). Preliminary tests indicate that it's working as intended :)

jaraco commented 7 years ago

I'm reluctant to see a boolean flag with several touch points. Plus, the change invalidates the docstring and namesake of process_forever. I have another idea.

jaraco commented 7 years ago

What about simply writing your own event loop?

import irc.client
import functools

def invoke_while(condition, func):
    while condition:
        func()

items_to_process = [1, 2, 3]
client = irc.client.SimpleIRCClient()
client.connect(...)

process_once = functools.partial(client.reactor.process_once, timeout=0.2)
invoke_while(items_to_process, process_once)
igel-kun commented 7 years ago

yeah, I guess that would be better. Alternatively, I could throw some selfmade exception in on_disconnect I guess... Ok I'll drop the matter, thanks for the hints tho :)