Python 2.7 ha raggiunto la fine del supporto
e verrà ritirato
il 31 gennaio 2026. Dopo il ritiro, non potrai eseguire il deployment di applicazioni Python 2.7, anche se la tua organizzazione ha utilizzato in precedenza un criterio dell'organizzazione per riattivare i deployment di runtime legacy. Le tue applicazioni Python 2.7 esistenti continueranno a essere eseguite e a ricevere traffico dopo la
data di ritiro. Ti consigliamo di eseguire la migrazione all'ultima versione supportata di Python.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
I cursori di query consentono a un'applicazione di recuperare i risultati di una query in comodi batch e sono consigliati rispetto all'utilizzo di offset interi per la paginazione.
Per ulteriori informazioni sulla strutturazione delle query per la tua app, consulta Query.
Cursori di query
I cursori di query consentono a un'applicazione di recuperare i risultati di una query in comodi batch senza l'overhead di un offset di query. Dopo aver eseguito un'operazione di recupero, l'applicazione può ottenere un cursore, ovvero una stringa opaca codificata in base64 che indica la posizione dell'indice dell'ultimo risultato recuperato. L'applicazione può salvare questa stringa, ad esempio in Datastore, in Memcache, nel payload di un'attività della coda di lavoro o incorporata in una pagina web come parametro HTTP GET o POST e può quindi utilizzare il cursore come punto di partenza per un'operazione di recupero successiva per ottenere il batch successivo di risultati dal punto in cui è terminato il recupero precedente. Un recupero può anche specificare un cursore di fine per limitare l'ambito del set di risultati restituito.
Differenza tra offset e cursori
Anche se Datastore supporta gli offset interi, dovresti evitare di utilizzarli. Utilizza invece i cursori. L'utilizzo di un offset evita solo di restituire le entità saltate alla tua applicazione, ma queste entità vengono comunque recuperate internamente. Le entità ignorate influiscono sulla latenza della query e alla tua
applicazione vengono addebitate le operazioni di lettura necessarie per recuperarle. L'utilizzo di cursori anziché offset ti consente di evitare tutti questi costi.
Esempio di cursore di query
In Python, un'applicazione ottiene un cursore dopo aver recuperato i risultati della query chiamando il metodo cursor() dell'oggetto Query. Per recuperare risultati aggiuntivi dal punto del cursore, l'applicazione prepara una query simile con lo stesso tipo di entità, filtri e ordini di ordinamento e passa il cursore al metodo with_cursor() della query prima di eseguire il recupero:
fromgoogle.appengine.apiimportmemcachefromgoogle.appengine.extimportdb# class Person(db.Model): ...# Start a query for all Person entitiespeople=Person.all()# If the application stored a cursor during a previous request, use itperson_cursor=memcache.get('person_cursor')ifperson_cursor:people.with_cursor(start_cursor=person_cursor)# Iterate over the resultsforpersoninpeople:# Do something# Get updated cursor and store it for next timeperson_cursor=people.cursor()memcache.set('person_cursor',person_cursor)
Limitazioni dei cursori
I cursori sono soggetti alle seguenti limitazioni:
Un cursore può essere utilizzato solo dalla stessa applicazione che ha eseguito la query originale e solo per continuare la stessa query. Per utilizzare il cursore in un'operazione di recupero successiva, devi ricostituire esattamente la query originale, inclusi lo stesso tipo di entità, il filtro degli antenati, i filtri delle proprietà e gli ordini di ordinamento. Non è possibile recuperare i risultati utilizzando un cursore senza impostare la stessa query da cui è stato generato in origine.
Poiché gli operatori != e IN vengono implementati con più query, le query che li utilizzano non supportano i cursori.
I cursori non funzionano sempre come previsto con una query che utilizza un
filtro di disuguaglianza o un ordine di ordinamento per una proprietà con più valori. La logica di deduplica per queste proprietà con più valori non persiste tra i recuperi, pertanto lo stesso risultato potrebbe essere restituito più volte.
Le nuove release di App Engine potrebbero modificare i dettagli di implementazione interna, invalidando i cursori che dipendono da questi. Se un'applicazione tenta di utilizzare un cursore non più valido, Datastore genera un'eccezione BadRequestError.
Cursori e aggiornamenti dei dati
La posizione del cursore è definita come la posizione nell'elenco dei risultati dopo
l'ultimo risultato restituito. Un cursore non è una posizione relativa nell'elenco
(non è un offset); è un indicatore a cui Datastore può saltare
quando avvia una scansione dell'indice per i risultati. Se i risultati di una query cambiano tra un utilizzo e l'altro di un cursore, la query rileva solo le modifiche che si verificano nei risultati dopo il cursore. Se un nuovo risultato viene visualizzato prima della posizione del cursore per la query, non verrà restituito quando vengono recuperati i risultati successivi al cursore.
Analogamente, se un'entità non è più un risultato per una query, ma è stata visualizzata prima del cursore, i risultati visualizzati dopo il cursore non cambiano. Se
l'ultimo risultato restituito viene rimosso dal set di risultati, il cursore sa ancora
come trovare il risultato successivo.
Quando recuperi i risultati delle query, puoi utilizzare sia un cursore di inizio sia un cursore di fine per restituire un gruppo continuo di risultati da Datastore. Quando
utilizzi un cursore di inizio e di fine per recuperare i risultati, non è garantito
che le dimensioni dei risultati siano le stesse di quando hai generato i cursori.
Le entità possono essere aggiunte o eliminate da Datastore tra il momento in cui vengono generati i cursori e il momento in cui vengono utilizzati in una query.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eQuery cursors allow applications to retrieve query results in batches, marking the position of the last retrieved result as an opaque, base64-encoded string.\u003c/p\u003e\n"],["\u003cp\u003eUsing cursors instead of integer offsets is recommended because offsets still retrieve skipped entities internally, affecting latency and incurring read operation costs.\u003c/p\u003e\n"],["\u003cp\u003eCursors can be saved and used later to continue a query from where it left off, allowing applications to get the next batch of results.\u003c/p\u003e\n"],["\u003cp\u003eCursors have limitations, such as only being usable by the same application for the same query and not supporting \u003ccode\u003e!=\u003c/code\u003e and \u003ccode\u003eIN\u003c/code\u003e operators.\u003c/p\u003e\n"],["\u003cp\u003eThe position of a cursor is defined as the location after the last returned result, and while it is not affected by results that come before the cursor, new additions or removals after the cursor can impact results.\u003c/p\u003e\n"]]],[],null,["# Query Cursors\n\n*Query cursors* allow an application to retrieve a query's results in convenient\nbatches, and are recommended over using integer offsets for pagination.\nSee [Queries](/appengine/docs/legacy/standard/python/datastore/queries) for more information on structuring queries for your app.\n\nQuery cursors\n-------------\n\n*Query cursors* allow an application to retrieve a query's results in convenient\nbatches without incurring the overhead of a query offset. After performing a\n[retrieval operation](/appengine/docs/legacy/standard/python/datastore/retrieving-query-results), the application can obtain a\ncursor, which is an opaque base64-encoded string marking the index position of\nthe last result retrieved. The application can save this string, for example in\nDatastore, in Memcache, in a Task Queue task payload, or embedded in\na web page as an HTTP `GET` or `POST` parameter, and can then use the cursor as\nthe starting point for a subsequent retrieval operation to obtain the next batch\nof results from the point where the previous retrieval ended. A retrieval can\nalso specify an end cursor, to limit the extent of the result set returned.\n\nOffsets versus cursors\n----------------------\n\nAlthough Datastore supports integer offsets, you should avoid\nusing them. Instead, use cursors. Using an offset only avoids returning the\nskipped entities to your application, but these entities are still retrieved\ninternally. The skipped entities do affect the latency of the query, and your\napplication is billed for the read operations required to retrieve them. Using\ncursors instead of offsets lets you avoid all these costs.\n\nQuery cursor example\n--------------------\n\nIn Python, an application obtains a cursor after retrieving query results by\ncalling the `Query` object's [`cursor()`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_cursor) method. To\nretrieve additional results from the point of the cursor, the application\nprepares a similar query with the same entity kind, filters, and sort orders,\nand passes the cursor to the query's\n[`with_cursor()`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_with_cursor) method before performing the\nretrieval: \n\n from google.appengine.api import memcache\n from google.appengine.ext import db\n\n # class Person(db.Model): ...\n \n # Start a query for all Person entities\n people = Person.all()\n \n # If the application stored a cursor during a previous request, use it\n person_cursor = memcache.get('person_cursor')\n if person_cursor:\n people.with_cursor(start_cursor=person_cursor)\n \n # Iterate over the results\n for person in people:\n # Do something\n \n # Get updated cursor and store it for next time\n person_cursor = people.cursor()\n memcache.set('person_cursor', person_cursor)\n\n| **Note:** Because of the way the iterator interface retrieves results in batches, getting a cursor may result in an additional call to Datastore to position the cursor where the iterator left off. If using only a start cursor, and if you know how many results you need ahead of time, it's faster to use [`fetch()`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_fetch).\n| **Caution:** Be careful when passing a Datastore cursor to a client, such as in a web form. Although the client cannot change the cursor value to access results outside of the original query, it is possible for it to decode the cursor to expose information about result entities, such as the application ID, entity kind, key name or numeric ID, ancestor keys, and properties used in the query's filters and sort orders. If you don't want users to have access to that information, you can encrypt the cursor, or store it and provide the user with an opaque key.\n\n### Limitations of cursors\n\nCursors are subject to the following limitations:\n\n- A cursor can be used only by the same application that performed the original query, and only to continue the same query. To use the cursor in a subsequent retrieval operation, you must reconstitute the original query exactly, including the same entity kind, ancestor filter, property filters, and sort orders. It is not possible to retrieve results using a cursor without setting up the same query from which it was originally generated.\n- Because the `!=` and `IN` operators are implemented with multiple queries, queries that use them do not support cursors.\n- Cursors don't always work as expected with a query that uses an inequality filter or a sort order on a property with multiple values. The de-duplication logic for such multiple-valued properties does not persist between retrievals, possibly causing the same result to be returned more than once.\n- New App Engine releases might change internal implementation details, invalidating cursors that depend on them. If an application attempts to use a cursor that is no longer valid, Datastore raises a [`BadRequestError`](/appengine/docs/legacy/standard/python/datastore/exceptions#BadRequestError) exception.\n\n### Cursors and data updates\n\nThe cursor's position is defined as the location in the result list after the\nlast result returned. A cursor is not a relative position in the list\n(it's not an offset); it's a marker to which Datastore can jump\nwhen starting an index scan for results. If the results for a query change\nbetween uses of a cursor, the query notices only changes that occur in results\nafter the cursor. If a new result appears before the cursor's position for the\nquery, it will not be returned when the results after the cursor are fetched.\nSimilarly, if an entity is no longer a result for a query but had appeared\nbefore the cursor, the results that appear after the cursor do not change. If\nthe last result returned is removed from the result set, the cursor still knows\nhow to locate the next result.\n\nWhen retrieving query results, you can use both a start cursor and an end cursor\nto return a continuous group of results from Datastore. When\nusing a start and end cursor to retrieve the results, you are not guaranteed\nthat the size of the results will be the same as when you generated the cursors.\nEntities may be added or deleted from Datastore between the\ntime the cursors are generated and when they are used in a query.\n\nWhat's next?\n------------\n\n- [Learn how to specify what a query returns and further control query\n results](/appengine/docs/legacy/standard/python/datastore/retrieving-query-results).\n- Learn the [common restrictions](/appengine/docs/legacy/standard/python/datastore/query-restrictions) for queries on Datastore.\n- [Understand data consistency](/appengine/docs/legacy/standard/python/datastore/data-consistency) and how data consistency works with different types of queries on Datastore.\n- Learn the [basic syntax and structure of queries](/appengine/docs/legacy/standard/python/datastore/queries) for Datastore."]]