sdgathman on Nostr: C-Python can link with and interact with multi-threaded C code no problem. See for an ...
C-Python can link with and interact with multi-threaded C code no problem. See
https://github.com/sdgathman/pymilter/blob/master/miltermodule.c for an example of the C code end (key object is PyThreadState).
C-Python code itself does not multi-thread, except through multi-processing. (Jython is a Java python implementation that does multi-thread.)
HOWEVER, what is ultimately more efficient than multi-threading for pure python or most other interpreted code is co-routines. Remember those from Knuth's "Fundamental Algorithms"? Python has built-in support for very clean and efficient co-routines - key syntax is the "yield" statement. They work very well ad-hoc, and there are frameworks like the "twisted" library that provide consistent interaction between many parts.
The key limitation of co-routines is that other "threads" do not have a chance to run until the current code hits a "yield". This also means you don't need to bother with locks and stuff like with true multi-tasking, hence the efficiency for an interpreted language.
Another name for this is "cooperative multi-threading". It is harder to do in a language like C with linear stacks. Python stack frames are linked lists - and the resulting conceptual tangle of stack frames when hundreds of complex co-routine tasks cooperate gave rise to the name of the "twisted" library.
Published at
2023-08-10 16:29:11Event JSON
{
"id": "e1a459d6de5ac79c4992175bc8508907afa256b07d3828fa9903937b802afae4",
"pubkey": "300ec228d0823779c529dc87278dea4b804cb258ef0f4ae6542125ee0222829e",
"created_at": 1691684951,
"kind": 1,
"tags": [
[
"p",
"ee152f951a3ada50c33086313202cca64af5ed00a67520ea35bb624330a1d877",
"wss://relay.mostr.pub"
],
[
"p",
"12da98a3a66fb47b43526b1b241e91df632bfd4058dc5364938dc932fe931118",
"wss://relay.mostr.pub"
],
[
"p",
"78987f6120558c5ea534493ccd05b098f0364012f40ea7f89f9419bdd5c67aeb",
"wss://relay.mostr.pub"
],
[
"e",
"e2e82578af26e82d3f749cf15c85b2b5d117a835b6eb46ca222c29bdb32e75de",
"wss://relay.mostr.pub",
"reply"
],
[
"proxy",
"https://chatter.llamarific.social/objects/160c117c-f0b8-46f5-b96a-94a903e04fba",
"activitypub"
]
],
"content": "C-Python can link with and interact with multi-threaded C code no problem. See https://github.com/sdgathman/pymilter/blob/master/miltermodule.c for an example of the C code end (key object is PyThreadState). \n\nC-Python code itself does not multi-thread, except through multi-processing. (Jython is a Java python implementation that does multi-thread.) \n\nHOWEVER, what is ultimately more efficient than multi-threading for pure python or most other interpreted code is co-routines. Remember those from Knuth's \"Fundamental Algorithms\"? Python has built-in support for very clean and efficient co-routines - key syntax is the \"yield\" statement. They work very well ad-hoc, and there are frameworks like the \"twisted\" library that provide consistent interaction between many parts.\n\nThe key limitation of co-routines is that other \"threads\" do not have a chance to run until the current code hits a \"yield\". This also means you don't need to bother with locks and stuff like with true multi-tasking, hence the efficiency for an interpreted language. \n\nAnother name for this is \"cooperative multi-threading\". It is harder to do in a language like C with linear stacks. Python stack frames are linked lists - and the resulting conceptual tangle of stack frames when hundreds of complex co-routine tasks cooperate gave rise to the name of the \"twisted\" library.",
"sig": "c780f903efa5bfc6b8a326066407d82cd0eff51c248e88fbd78af82a288fc56c611a3250fcc9090ef5f26c2d4a654037aa8bdd53444711e660a45464b2c20c28"
}