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

  • Home
  • SEARCH
  • 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 6870311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:40:06+00:00 2026-05-27T03:40:06+00:00

My goal here is to make a simple server handling TCP message like process

  • 0

My goal here is to make a simple server handling TCP message like “process My String” that send “My String” to be processed by a quite time taking operation called (here slowFunction). Here I’m calling this function through deferToThread but nothing seems to happened : the defered’s callback messages doesn’t show up anywhere (breakpoints show it’s never called) and the print in the function aren’t displayed (breakpoints show it’s never called)

from twisted import protocols
from twisted.protocols import basic
from twisted.internet import threads, protocol, reactor
from twisted.application import service, internet
import re
import time

def slowFunction(arg):
    print "starting process"
    time.sleep(1)
    print "processed "+arg

class MySimpleServer(basic.LineReceiver):

    PROCESS_COMMAND = "process (.*)" #re pattern
    processFunction = slowFunction
    clients = []

    def connectionMade(self):
        print "Client connected"
        MySimpleServer.clients.append(self)

    def connectionLost(self, reason):
        print "Client gone"
        MySimpleServer.clients.remove(self)

    def onProcessDone(self):
        self.message("Process done")

    def onError(self):
        self.message("Process fail with error")

    def lineReceived(self, line):
        processArgumentResult = re.search(MySimpleServer.PROCESS_COMMAND, line)
        if not processArgumentResult == None:
            processArgument = processArgumentResult.groups()[0] 
            deferred = threads.deferToThread(MySimpleServer.processFunction, processArgument)
            deferred.addCallback(self.onProcessDone)
            deferred.addErrback(self.onError)
            self.message("processing your request")
        else:
            print "Unknown message line: "+line

    def message(self, message):
        self.transport.write(message + '\n')

if __name__ == '__main__':
    factory = protocol.ServerFactory()
    factory.protocol = MySimpleServer
    factory.client = []

    reactor.listenTCP(8000, factory)
    reactor.run()
  • 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-27T03:40:07+00:00Added an answer on May 27, 2026 at 3:40 am

    I’ve got some help by the guys of twisted irc

    The points are : the callback (onProcessDone and onError) are supposed to take a result argument, and the function called by deferToThread will receive self as an argument (it should one of the MySimpleServer class’ method).

    The final code is now :

    from twisted import protocols
    from twisted.protocols import basic
    from twisted.internet import threads, protocol, reactor
    from twisted.application import service, internet
    import re
    import time
    
    def slowFunction(arg):
        print "starting process"
        time.sleep(20)
        print "processed "+arg
    
    class MySimpleServer(basic.LineReceiver):
    
        PROCESS_COMMAND = "process (.*)" #re pattern
        clients = []
    
        def connectionMade(self):
            print "Client connected"
            MySimpleServer.clients.append(self)
    
        def connectionLost(self, reason):
            print "Client gone"
            MySimpleServer.clients.remove(self)
    
        def onProcessDone(self, result):
            self.message("Process done")
    
        def onError(self, result):
            self.message("Process fail with error")
    
        def processFunction(self, processArgument):
            slowFunction(processArgument)
    
        def lineReceived(self, line):
            processArgumentResult = re.search(MySimpleServer.PROCESS_COMMAND, line)
            if not processArgumentResult == None:
                processArgument = processArgumentResult.groups()[0] 
                deferred = threads.deferToThread(self.processFunction, processArgument)
                deferred.addCallback(self.onProcessDone)
                deferred.addErrback(self.onError)
                self.message("processing your request")
            else:
                print "Unknown message line: "+line
    
        def message(self, message):
            self.transport.write(message + '\n')
    
    if __name__ == '__main__':
        factory = protocol.ServerFactory()
        factory.protocol = MySimpleServer
        factory.client = []
    
        reactor.listenTCP(8000, factory)
        reactor.run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal here is to make a button. I want the text to sit
My goal here is to create a very simple template language. At the moment,
For example, my goal is to test the code given here: PHP script that
My goal is to make a module that will make a grid on a
Assuming that I wanted to make a simple dos-based game and I wanted to
My goal here is to open temp.php, explode by ### (line terminator), then by
So my goal here is to have a single search field in an application
I have a project here the goal is to merge multiple Access DB's into
Here's the goal: to replace all standalone ampersands with & but NOT replace those
Here is my ultimate goal... to take this xml file.. <?xml version=1.0?> <Songs> <Song>

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.