You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since pickle is the standard serializer, you are apparently using a custom serializer. So it is up to you how to serialize exceptions.
Perhaps I misunderstand your question?
Since pickle is the standard serializer, you are apparently using a custom serializer. So it is up to you how to serialize exceptions. Perhaps I misunderstand your question?
How can I serialize exceptions to JSON when the job failed?
I think what the OP meant to ask was how to serialize the exception when job.result() is supposed to reraise it but can't due to serialization.
I'm on version 0.25.0. When job.result() is called on a job using the msgpack deserializer, it immediately fails with arq.jobs.SerializationError: unable to serialize result. From the code, deserialization happens before reraising. And since msgpack cannot serialize an exception, info.result will not be an Exception.
As a serializer, msgpack cannot deal with the case where the job failed due to an exception.
Reacted by Weiliang Li, joshwilson-dbx and Einar Wigum Arbo
Found a workaround when using msgpack: you can define your custom serialization function that checks if the 'r' field (result) in the dict passed to it is of type Exception. If it is, you can override that field with the str repr of the exception so that msgpack can serialize it. This is how I do it:
defjob_msgpack_serialize(obj: dict[str, Any]) ->bytes:
if"r"inobjandisinstance(obj["r"], Exception):
obj["r"] =repr(obj["r"]) # make sure exceptions are serializable by msgpackreturnmsgpack.packb(obj)
Activity
iamlikeme commentedon Nov 19, 2022
Since pickle is the standard serializer, you are apparently using a custom serializer. So it is up to you how to serialize exceptions.
Perhaps I misunderstand your question?
kigawas commentedon Nov 24, 2022
How can I serialize exceptions to JSON when the job failed?
iamlikeme commentedon Dec 11, 2022
How to serialize an exception to JSON is a very generic question outside the scope of arq. StackOverflow has some suggestion.
heshiming commentedon Aug 9, 2023
I think what the OP meant to ask was how to serialize the exception when
job.result()is supposed to reraise it but can't due to serialization.I'm on version 0.25.0. When
job.result()is called on ajobusing themsgpackdeserializer, it immediately fails witharq.jobs.SerializationError: unable to serialize result. From the code, deserialization happens before reraising. And sincemsgpackcannot serialize an exception,info.resultwill not be anException.As a serializer,
msgpackcannot deal with the case where the job failed due to an exception.SimonDahrs commentedon Feb 5, 2025
Found a workaround when using msgpack: you can define your custom serialization function that checks if the 'r' field (result) in the dict passed to it is of type Exception. If it is, you can override that field with the str repr of the exception so that msgpack can serialize it. This is how I do it: