6 ms·
Great article. I had the misfortune of writing a bunch of twisted code over a decade ago and I wanted to remind the author that twisted has a feature called "in
by shanemhansen 3y ago
Great article. I had the misfortune of writing a bunch of twisted code over a decade ago and I wanted to remind the author that twisted has a feature called "inline callbacks" that allow you to use yield. https://twisted.org/documents/16.4.1/core/howto/defer-intro.html#inline-callbacks-using-yield https://twisted.org/documents/16.4.1/core/howto/defer-intro....
So twisted code can actually look like:
@inlineCallbacks
def doIt():
responseBody = yield makeRequest("GET", "/users")
returnValue(json.loads(responseBody))
iirc `returnValue` throws an exception of a specific type. It's ugly, but it's also the logical implementation of async on top of yield/generators.
- silverdrake11 3y agoNowadays twisted supports this syntax: async def doIt(): responseBody = await makeRequest("GET", "/users") return json.loads(responseBody) See https://patrick.cloke.us/posts/2021/06/11/converting-twisteds-inlinecallbacks-to-async https://patrick.cloke.us/posts/2021/06/11/converting-twisted...
- BiteCode_dev 3y agoAlso, asyncio started like this as well.
- plq 3y agoreturnValue raises a specific exception that inlineCallbacks understands and translates to a normal return value. It was a hack that was only needed for python versions that couldn't have generators return non-None values. With modern versions, you don't need it anymore -- just use return.
- zokier 3y agoWhile inlineCallbacks exists, and indeed works, I would recommend not using it. I recall it not interacting with mypy particularly well, and in general being such a hack it had some annoying sharp edges. Explicit callbacks might be annoying but at least they are extremely clear on what's happening.
- deleted 3y ago[deleted]
- bevenky 3y agoThis is so nostalgic. I actually met my cofounder on github due to a discussion on twisted vs gevent back in 2011. I had my inital code in twisted and he wrote the gevent piece. Fast forward 12 years and we still use gevent at http://plivo.com http://plivo.com :) Some of our initial code snippets: # Twisted def __protocolSendRaw(self, name, args=""): deferred = defer.Deferred() self.__EventQueue.append((name, deferred)) self.rawSend("%s %s" % (name, args)) return deferred # Gevent def _protocol_sendmsg(self, name, args=None, async=False): if self._closing_state: return Event() _async_res = gevent.event.AsyncResult() _uuid, event = _async_res.get() return event
- kldx 3y agoOff topic: PLIVO, the norwegian term actually is a protocol used by critical services here. Thought you might find it interesting :) > PLIVO (an abbreviation for ongoing life-threatening violence) is a procedure for cooperation between the police, the fire service, the rescue service and the healthcare system in incidents where life-threatening violence is perpetrated against several people.
- bevenky 3y agoNice, did not know this.. Plivo in latvian means flying high, thats was one of the languages we named it based on.
- joshxyz 3y agodamn i had memories of using plivo back in 2012 2013 2014
- lelandbatey 3y agoMy last job (2019 - 2022) was in part shepherding a 2013-era Python 2 Tornado-and-Motor (yes the async implementation of the MongoDB driver for Python) application into Python 3 and such modern niceties. Absolutely fuck yield-raise based async implementations. Just remembering those induces psychic damage; implementing async as a massive hack on top of the language is impressive but also the kind of thing we're going to whisper to junior engineers as scary campfire stories for DECADES.