Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 745527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:35:36+00:00 2026-05-14T09:35:36+00:00

I’m trying to write a dead-simple interface for an IRC library, like so: import

  • 0

I’m trying to write a dead-simple interface for an IRC library, like so:

import simpleirc

connection = simpleirc.Connect('irc.freenode.net', 6667)
channel = connection.join('foo')
find_command = re.compile(r'google ([a-z]+)').findall

for msg in channel:
    for t in find_command(msg):
        channel.say("http://google.com/search?q=%s" % t)

Working from their example, I’m running into trouble (code is a bit lengthy, so I pasted it here). Since the call to channel.__next__ needs to be returned when the callback <IRCClient instance>.privmsg is called, there doesn’t seem to be a clean option. Using exceptions or threads seems like the wrong thing here, is there a simpler (blocking?) way of using twisted that would make this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T09:35:37+00:00Added an answer on May 14, 2026 at 9:35 am

    In general, if you’re trying to use Twisted in a “blocking” way, you’re going to run into a lot of difficulties, because that’s neither the way it’s intended to be used, nor the way in which most people use it.

    Going with the flow is generally a lot easier, and in this case, that means embracing callbacks. The callback-style solution to your question would look something like this:

    import re
    from twisted.internet import reactor, protocol
    from twisted.words.protocols import irc
    
    find_command = re.compile(r'google ([a-z]+)').findall
    
    class Googler(irc.IRCClient):
        def privmsg(self, user, channel, message):
            for text in find_command(message):
                self.say(channel, "http://google.com/search?q=%s" % (text,))
    
    def connect():
        cc = protocol.ClientCreator(reactor, Googler)
        return cc.connectTCP(host, port)
    
    def run(proto):
        proto.join(channel)
    
    def main():
        d = connect()
        d.addCallback(run)
        reactor.run()
    

    This isn’t absolutely required (but I strongly suggest you consider trying it). One alternative is inlineCallbacks:

    import re
    from twisted.internet import reactor, protocol, defer
    from twisted.words.protocols import irc
    
    find_command = re.compile(r'google ([a-z]+)').findall
    
    class Googler(irc.IRCClient):
        def privmsg(self, user, channel, message):
            for text in find_command(message):
                self.say(channel, "http://google.com/search?q=%s" % (text,))
    
    @defer.inlineCallbacks
    def run():
        cc = protocol.ClientCreator(reactor, Googler)
        proto = yield cc.connectTCP(host, port)
        proto.join(channel)
    
    def main():
        run()
        reactor.run()
    

    Notice no more addCallbacks. It’s been replaced by yield in a decorated generator function. This could get even closer to what you asked for if you had a version of Googler with a different API (the one above should work with IRCClient from Twisted as it is written – though I didn’t test it). It would be entirely possible for Googler.join to return a Channel object of some sort, and for that Channel object to be iterable like this:

    @defer.inlineCallbacks
    def run():
        cc = protocol.ClientCreator(reactor, Googler)
        proto = yield cc.connectTCP(host, port)
        channel = proto.join(channel)
        for msg in channel:
            msg = yield msg
            for text in find_command(msg):
                channel.say("http://google.com/search?q=%s" % (text,))
    

    It’s only a matter of implementing this API on top of the ones already present. Of course, the yield expressions are still there, and I don’t know how much this will upset you. 😉

    It’s possible to go still further away from callbacks and make the context switches necessary for asynchronous operation to work completely invisible. This is bad for the same reason it would be bad for sidewalks outside your house to be littered with invisible bear traps. However, it’s possible. Using something like corotwine, itself based on a third-party coroutine library for CPython, you can have the implementation of Channel do the context switching itself, rather than requiring the calling application code to do it. The result might look something like:

    from corotwine import protocol
    
    def run():
        proto = Googler()
        transport = protocol.gConnectTCP(host, port)
        proto.makeConnection(transport)
        channel = proto.join(channel)
        for msg in channel:
            for text in find_command(msg):
                channel.say("http://google.com/search?q=%s" % (text,))
    

    with an implementation of Channel that might look something like:

    from corotwine import defer
    
    class Channel(object):
        def __init__(self, ircClient, name):
            self.ircClient = ircClient
            self.name = name
    
        def __iter__(self):
            while True:
                d = self.ircClient.getNextMessage(self.name)
                message = defer.blockOn(d)
                yield message
    

    This in turn depends on a new Googler method, getNextMessage, which is a straightforward feature addition based on existing IRCClient callbacks:

    from twisted.internet import defer
    
    class Googler(irc.IRCClient):
        def connectionMade(self):
            irc.IRCClient.connectionMade(self)
            self._nextMessages = {}
    
        def getNextMessage(self, channel):
            if channel not in self._nextMessages:
                self._nextMessages[channel] = defer.DeferredQueue()
            return self._nextMessages[channel].get()
    
        def privmsg(self, user, channel, message):
            if channel not in self._nextMessages:
                self._nextMessages[channel] = defer.DeferredQueue()
            self._nextMessages[channel].put(message)
    

    To run this, you create a new greenlet for the run function and switch to it, and then start the reactor.

    from greenlet import greenlet
    
    def main():
        greenlet(run).switch()
        reactor.run()
    

    When run gets to its first asynchronous operation, it switches back to the reactor greenlet (which is the “main” greenlet in this case, but it doesn’t really matter) to let the asynchronous operation complete. When it completes, corotwine turns the callback into a greenlet switch back into run. So run is granted the illusion of running straight through, like a “normal” synchronous program. Keep in mind that it is just an illusion, though.

    So, it’s possible to get as far away from the callback-oriented style that is most commonly used with Twisted as you want. It’s not necessarily a good idea, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.