summaryrefslogtreecommitdiff
path: root/docs/howto/remove_process.rst
blob: 26d54a62ed54530ae5708bcfc533d4808a2fc88a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
.. _remove-process:

How to remove xmpp.process()
============================


Starting from slixmpp 1.8.0, running ``process()`` on an
XMLStream/ClientXMPP/ComponentXMPP instance is deprecated, and starting from
1.9.0, it will be removed.

Why
---

This has been the usual way of running an application using SleekXMPP/slixmpp
for ages, but it has come at a price: people do not understand how they
should run their application without it, or how to integrate their slixmpp
code with the rest of their asyncio application.

In essence, ``process()`` is only a very thin wrapper around asyncio loop
functions:

.. code-block:: python

        if timeout is None:
            if forever:
                self.loop.run_forever()
            else:
                self.loop.run_until_complete(self.disconnected)
        else:
            tasks: List[Future] = [asyncio.sleep(timeout)]
            if not forever:
                tasks.append(self.disconnected)
            self.loop.run_until_complete(asyncio.wait(tasks))

How
---

Hence it can be replaced according to what you want your application to do:

- To run forever, ``loop.run_forever()`` will work just fine

- To run until disconnected, ``loop.run_until_complete(xmpp.disconnected)``
  will be enough (XMLStream.disconnected is an future which result is set when
  the stream gets disconnected.

- To run for a scheduled time (and still abort when disconnected):

.. code-block:: python

    tasks = [asyncio.sleep(timeout)]
    tasks.append(xmpp.disconnected)
    loop.run_until_complete(asyncio.wait(tasks))

There is no magic at play here and anything is possible if a more flexible
execution scheme is expected.