Skip to content

Instantly share code, notes, and snippets.

@adiroiban
Last active February 13, 2019 17:37
Show Gist options
  • Save adiroiban/d2bb96a52634d7aa07493ac3df2ecfb0 to your computer and use it in GitHub Desktop.
Save adiroiban/d2bb96a52634d7aa07493ac3df2ecfb0 to your computer and use it in GitHub Desktop.
Ampoule Example from main thread
"""
This code is launched in the child process.
It is also loaded/imported in the main process for AMP protocol to know the
available command, arguments and results.
Each process will use about 30MB of ram, as this is how much Twisted uses.
Having more imports here, will increase the size of the process.
"""
import os
from ampoule import child
from twisted.protocols import amp
from twisted.conch.ssh.common import _MPpow
class Pid(amp.Command):
"""
A simple command which takes no argument and return the PID of the
current process.
"""
response = [('pid', amp.Integer())]
class MPPow(amp.Command):
"""
Computes x to the power of y modulus z, for very large numbers.
"""
arguments = [
('x', amp.Integer()),
('y', amp.Integer()),
('z', amp.Integer()),
]
response = [('result', amp.String())]
class ChevahAmpouleChild(child.AMPChild):
"""
Code that runs in the Ampoule child worker process.
"""
def __init__(self, *args, **kwargs):
super(ChevahAmpouleChild, self).__init__(*args, **kwargs)
try:
from setproctitle import setproctitle
setproctitle
except ImportError:
def setproctitle(name):
"""
Does nothing.
"""
setproctitle('twisted-ampoule-worker')
@Pid.responder
def pid(self):
return {"pid": os.getpid()}
@MPPow.responder
def mppow(self, x, y, z):
return {"result": _MPpow(x, y, z)}
#
# Main code executed from the main process and main Twisted thread.
#
from ampoule import pool
from ampoule import child, util, main
from twisted.protocols import amp
from twisted.internet import reactor, defer
from amp_child import ChevahAmpouleChild, Pid, MPPow
from twisted.conch.ssh.common import _MPpow
# We use large numbers as this operation takes some time.
y = 593963192664892095842236497038218948983062096628649649325878086854524285684776262901005567945578713706799483278149268382548494194073647814997294130916283944624772389721598145814599518767444593542791839787395975402058701130464086358354546764061657673749193895167214928293205561691463102219485745268238419584137756565916126289096622154740567824288608146594035157751685994281766579208162038822531676770398174769622588069835666805964024546537648230856627011872197398247587187253042717132956092384273826519979273032521811531535188283444238294916768742726275600469089380374270422246679062205939485970511774122622243853644122871819770141577060638307173522369135401396556619817265705931430668253942416882712682638403864714947345116917127542282708973492299237758663494129285412711093171304742587799319275635967579976339309624202246745296942308974073462516512095323904457847336733034611439076664018506394648494647266484265251526901310380697408922700243540555277684311547943003707334194111106844312686715824754230699169482141187943302051166295899340515757871815043161153915503195018785209083131399753177450622734141180784334743109734757499493595144049914628817376606224911794031747051434372057951513388582794368009888876022794002592005102846963514642907101387533079804322926845121128680792129658683333424331710437709020951446558246418249322491671935195707753105680770020419711375236362903460516921806304013045003775062881801768390562147767941139606813529892275122637146660145578984379209261019701159821902393795527215252029851252163809554101360318625232226229061294923646063937464523230712077036569671957513226493915120995092643368848985884657963035565043824208509115996048757263260479690990741183372620528199417759760611089616255397321972458339780815858772566691268752938230180198293318505131683134130053454209264218638985343596758320008713199373981212896889146940877034581862956229735290887739023960228136029879328930151348538087392273124432169069984550984343028342454145631400588295752167187108033877976818545259029296950920238673907208855616514560755471922191132436519602142237235515009242093825212841719835002193950363146096174382228740732443190946622088928162593637288809107040279749576784584390756095693937323995057885599694864954097338552781170140227895790777827172134790102810626239792148560568584311986794461640265772857613636932035363805539352430372148116562839405925232804722039849942546523337181928898668083368717611360319764039290173653152250983965002508265609973L
z = 898321079887949024873846298829829803385675792558103427627249151427553752170683527309237758866718388700808412104558526338186843771269895657657946005378105676689380973925718347712336010086740172202998184119333582598949361646916364829155921871035926337265650154160133445053110358475514180687798798727766278832371222102202271071623857804701696909940435035803265804327385452209869455276903547636639729863151867359673146970543467299066033095260323071204298240331859668081578512283593103997487200373538324909702842235426849418969325774443162203199664850534670034487608846718780576668243532940182408441624754675686017965768321355760240678796684807699043686224319012981967387202701225294531221434272217328050822793644847186329102183777579809258903170197692835420304283854553983567793832328786640276013790212995277061055354254912253576243275676827919277459445140054926724389612827290303881361641037381047349389094058428068139579137882187212730171889799816393153486120574125373306986510733903047174989513810128910100771187159146049555770596597034446058133309918435859674467410321980515027477260347996328081672091597403499229625388611534507582821485180609940962685336641438614146210803660399722021119866727540121198787644984601666687403027266018935033790447190100509619636721802309991656051437569254522474247828966170474373161846304068936576431280751424914119546438981391615991630377463568417944446927314599067355299489779943126862252953558316092568606418015157021827163220946555718863221291635642426861132837044141637632642520213603779760352684787513324581542595679678951981447349369840496135670784249508269949585745399462849083808287109431823518948483493306226457763205652344323876400463356226696408264470817774593545668640454445410068352977836664990632438550074053253068864979658788382176152510968355927508838519285793705513841206046714472908356636519426180804937873250687054733603950652420738060096550688502811205035515986480838798768589183586486916598656699413475015387844814125965306700415305089021314677231744207127326505272546598432681395724968249947524702189413015483044164178921844358754796314818767027637746794097894870172216751792713717937887165171615070870363987921576944918835896772475132163632091930105141032083303289656574667752910310469429204929202218437618367070588363792954433390073796990747432842835159508081544330372282343006980926884000636713664805779312528781134160946909488783473466020942733359285490092276627784400014966786332071196306304134918068517643L
# We use inlineCallback to make the code easier to use, but this can also
# work with normal handling.
# No need for special ampoule code.
@defer.inlineCallbacks
def _run():
expected = _MPpow(1, 2, 3)
pp = pool.ProcessPool(ChevahAmpouleChild, min=1, max=10)
yield pp.start()
result = yield pp.doWork(Pid)
print("The Child process PID is:", result['pid'])
t1 = pp.doWork(
MPPow,
x=2,
y=y,
z=z,
)
t2 = pp.doWork(
MPPow,
x=2,
y=y,
z=z,
)
result = yield t1
result = yield t2
print("The Child mppow:", result['result'])
yield pp.stop()
reactor.stop()
# In your real application, the reactor is already started.
reactor.callLater(1, _run)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment