Here's a very simple example I borrowed from

http://blog.iharder.net/2017/02/03/python-asyncio-and-tkinter-together-with-tcp-and-udp/:

Here is the raw code:
https://pastebin.com/raw/ZGeDULR9

This listens for tcp messages on port 9999 and prints it to a Tkinter
label as and when the message arrives.

I tested this on Python 3.6.3 on Linux.

To test it, here's how you send a TCP message on Linux command line:

echo 'some random message' > /dev/tcp/127.0.0.1/9999

Also note that in the above example - the async i/o loop is run in a
separate thread. But that is just one thread to handle and it is much
better tan handling one new thread for every new request.

As regards Michael Lange comment - while threads are easier to grasp
initially - they are not very scalable.
Imagine having hundreds of thread fetching messages simultaneously -
context switching at that level would be a nightmare.
Better than thread would be spawning a new process - but that too has its limit.

Spawning a new process is what Apache webserver does for handling each
request - but that means several thousands of processes run for a busy
server.
That soon exhausted a lot of CPU and became impractical for servers
with say a 10000 simultaneous visitors.

Then came NGINX server - which manages requests asynchronously -
exactly like using asyncio.
Much more scalable and soon high traffic sites dumped Apache in favor of NGINX.

Bottom line - if scalability is not your concern - threading or
spawning a new process could be OK but asyncio is a better approach
for the kind of thing you are trying to do.

On 12/20/17, c.bu...@posteo.jp <c.bu...@posteo.jp> wrote:
> On 2017-12-20 00:23 Michael Lange <klappn...@web.de> wrote:
>> to me like it might be much easier to just download the files from
>> within a separate thread. I don't know what exactly you want to
>> achieve of course, but from your first post my impression was that
>> using threads should not be overly difficult. I might be wrong there,
>> of course.
>
> I need to download between 100 or 200 xml-like files from different
> locations. More specific: I use "feedparser" to get rss/atom feeds.
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss@python.org
> https://mail.python.org/mailman/listinfo/tkinter-discuss
>
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to