Comments (2)
i don't know about the protocol part, but what you can do is to save the job request object being returned by the submit_job() method. This request object has a "state" attribute that tells you whether the job is created, failed, completed, etc., which you can query later easily simply by checking the value of request.state
from python-gearman.
I ran into this obstacle and came up with a solution. Even though the Gearman protocol allows this via GET_STATUS, the python-gearman implementation has, as far as I've seen, no method for doing it. My solution was to dig into the Gearman internals and craft a GET_STATUS packet, perhaps this can help someone else.
GET_STATUS
A client issues this to get status information for a submitted job.
Arguments:
- Job handle that was given in JOB_CREATED packet.
The GET_STATUS packet header is crafted as follows:
byte_size = struct.pack(">I", len(handle))
header = "\x00\x52\x45\x51\x00\x00\x00\x0f" + byte_size
If you are working in an environment with multiple Gearman job servers then you need to keep track of what host the specific job was sent to. You can detect that on the return of submit_job(), when it returns job.connection.gearman_host and .gearman.port.
The GET_STATUS packet requires you to append the handle which is returned as job.handle when you submit_job().
Once the GET_STATUS header has been crafted you need to send it to the appropriate job server like:
socket.send("%s%s" % (header, handle))
Keep in mind that GET_STATUS only works for background jobs, so the full solution would look something like this (untested, coding this example in Github's comment form for demonstration only):
job = gm.submit_job("reverse", "Hello world", wait_until_complete=False, background=True)
byte_size = struct.pack(">I", len(job.job.handle))
header = "\x00\x52\x45\x51\x00\x00\x00\x0f" + byte_size
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((job.job.connection.gearman_host, job.job.connection.gearman_port))
socket.send("%s%s" % (header, job.job.handle))
status = socket.recv(20 + len(job.job.handle))
socket.close()
The returned status (you are looking for the last 4 bytes) is then:
STATUS_RES
This is sent in response to a GET_STATUS request. This is used by
clients that have submitted a job with SUBMIT_JOB_BG to see if the
job has been completed, and if not, to get the percentage complete.Arguments:
- NULL byte terminated job handle.
- NULL byte terminated known status, this is 0 (false) or 1 (true).
- NULL byte terminated running status, this is 0 (false) or 1
(true).- NULL byte terminated percent complete numerator.
- Percent complete denominator.
Happy hacking :-)
PS: In the solution where you have to poll the request.state you need to store the entire object to be able to check statuses from other pids. The raw packet solution above works with any pid as long as it can access the host, port and handle. I personally keep track of these in Redis.
from python-gearman.
Related Issues (20)
- Worker intermittently lost connection with some gearmand servers HOT 3
- https://pypi.python.org/pypi/gearman/ HOT 6
- after_poll HOT 5
- Need option to force use of select()
- 100% CPU load after refactoring poll_connections_once HOT 1
- Get job_id from worker code HOT 1
- util.Stopwatch always returns "True" for has_time_remaining
- Feature Request: retrieve job result from client HOT 4
- how to specify a definite node where the job running on ?
- GearmanAdminClientClient.get_workers() is unable to parse worker IDs with spaces.
- Discard task on exception in decode step HOT 1
- Release v2.0.3 HOT 2
- how to get job status by handle
- KeyError occurs when we receivea job handle that is not in handle_to_request_map when the client is executing recv_* commands
- request in handle_to_request_map will not clear if backgroupd is True
- ipv6 support
- Crashing
- No idea how to run tests/Python 3 support HOT 3
- Should I use return or send_job_data()?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-gearman.