fromjinaimportClient, Documentfromjina.types.requestimportResponsedefprint_matches(resp: Response): # the callback function invoked when task is doneforidx, dinenumerate(resp.docs[0].matches[:3]): # print top-3 matchesprint(f'[{idx}]{d.scores["euclid"].value:2f}: "{d.text}"')
c=Client(protocol='http', port_expose=12345) # connect to localhost:12345c.post('/search', Document(text='request(on=something)'), on_done=print_matches)
,它打印以下结果:
Client@1608[S]:connected to the gateway at localhost:12345!
[0]0.168526: "@requests(on='/index')"
[1]0.181676: "@requests(on='/search')"
[2]0.192049: "query.matches = [Document(self._docs[int(idx)], copy=True, score=d) for idx, d in enumerate(dist)]"
# Schedule job to run at 9:15, October 10thjob=queue.enqueue_at(datetime(2019, 10, 8, 9, 15), say_hello)
# Schedule job to run in 10 secondsjob=queue.enqueue_in(timedelta(seconds=10), say_hello)
还支持重试失败的作业:
fromrqimportRetry# Retry up to 3 times, failed job will be requeued immediatelyqueue.enqueue(say_hello, retry=Retry(max=3))
# Retry up to 3 times, with configurable intervals between retriesqueue.enqueue(say_hello, retry=Retry(max=3, interval=[10, 30, 60]))
$ rq worker --with-scheduler*** Listening for work on defaultGot count_words_at_url('http://nvie.com') from defaultJob result = 818*** Listening for work on default