Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I know I've probably gotta use 2 threads to have both reading from the server and input simultaneous, which I can achieve, but only if it command line input. Mind you it is still logging the data from the channel at the same time.</p> </blockquote> <p>Actually, it's not necessary to use two threads for this. A major strength of Twisted is doing I/O without using threads. The <a href="https://stackoverflow.com/questions/8299896/keyboard-input-using-stdio-standardio-in-twisted-python">question and answer Michael linked to</a> talk about Twisted's non-threaded support for interacting with standard input and standard output via <code>twisted.internet.stdio.StandardIO</code>.</p> <blockquote> <p>My problem lies in not being able to figure out how to change this from just typing into and printing form the commandline; to being able to actually send messages to the irc server for other people to read.</p> </blockquote> <p><code>IRCClient</code> has a method for sending messages to the IRC server - the sample code you included in your question even uses this method already, <code>IRCClient.msg</code>. All you need to do is call it.</p> <p>Your threaded code could change like this to do so (again, threads are unnecessary, but in the interest of <strong>just</strong> showing you how to send a message from your input handling code, I'll base this part of the answer on threads, to avoid other changes to the code which might make the answer harder to understand. You <strong>do not</strong> need threads to do this.):</p> <pre><code>def aSillyBlockingMethod(bot): import time while True: msg = raw_input() bot.threadSafeMsg("#bottest", msg) class LogBot(irc.IRCClient): """A logging IRC bot.""" nickname = "twistedbot" def connectionMade(self): irc.IRCClient.connectionMade(self) self.logger = MessageLogger(open(self.factory.filename, "a")) self.logger.log("[connected at %s]" % time.asctime(time.localtime(time.time()))) # The bot is now connected. Start reading input here. # Pass a reference to this protocol instance, so that # messages can be sent to this protocol instance. deferToThread(aSillyBlockingMethod, self) # Define a helper function for aSillyBlockingMethod to use. # Since aSillyBlockingMethod runs in a thread, it cannot just call # IRCClient.msg, since that method - like almost all methods in Twisted - # is not thread-safe. Instead it must call this thread-safe wrapper. def threadSafeMsg(self, channel, message): reactor.callFromThread(self.msg, channel, message) </code></pre> <p>Notice that all that's happening here is that <code>aSillyBlockingMethod</code> is calling a method on <code>LogBot</code>. The thread-safe wrapper would not be necessary when using <code>StandardIO</code>, since that eliminates the need for threads.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload