Coder Social home page Coder Social logo

Comments (3)

nikipore avatar nikipore commented on August 20, 2024

Thanks for this heads-up. But I don't see where anything would run in a thread different from the main thread without explicitly telling the reactor to do so. Where do we leave the main thread in the first place? And if so, how do you suggest to fix this?

from stompest.

LiyiW avatar LiyiW commented on August 20, 2024

You are right, only by telling the reactor to do run in another thread can that happen.
I provide an example.
twisted_test_consumer.txt
twisted_test_producer.txt

Just change the suffix of the files to be .py and run them.
As you will see, the consumer logs "Sending ACK frame..." in a thread different from main thread.

In my message handler "consume", I tell the reactor to run "callback_func" in another thread because somehow it runs for a while and I hope multiple messages be processed in parallel. As a result, the rest of the "onMessage" function(stompest/src/async/stompest/async/listener.py line 207) will also run in that thread due to the mechanism of Deferred and reactor.

As we can't let ack run in main thread again, I suggest a lock be acquired at before telling twisted transport to write, just use lock in the StompProtocol object

in init:
self._lock = threading.RLock()

and do the following
with self._lock:
self.transport.write(binaryType(frame))
(stompest/src/async/stompest/async/protocol.py line 48)

I think this resolves the problem

from stompest.

nikipore avatar nikipore commented on August 20, 2024

Synchronizing the access to the transport level is exactly what you shouldn't do in an asynchronous (sic!) setting, so your suggested solution is invalid.

Looking at your message handler

@defer.inlineCallbacks
def consume(self, client, frame):
    d = defer.Deferred()
    d.addCallback(longRunningCalculation)
    reactor.callInThread(d.callback, "result 1")
    yield d

You are doing something very wicked, namely calling a callback from another thread, so you yield from one (the main) thread and send from another, which messes up the coroutine and imho breaks the reactor (which must remain single-threaded) altogether. If you do it this way (untested), it should work:

from twisted.internet import threads
[...]
@defer.inlineCallbacks
def consume(self, client, frame):
    result = yield threads.deferToThread(longRunningCalculation)
    logging.debug("In the main thread: %s", result)

from stompest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.