I decided to share one of my favorite tricks in Python's standard library through an example. The entire code runs on Python 3.2+ without external packages.
I decided to share one of my favorite tricks in Python's standard library through an example. The entire code runs on Python 3.2+ without external packages.The initial problem
Let's say you have a thousand URLs to process/download/examine, so you need to issue as much HTTP GET calls and retrieve the body of each response.
This is a way to do it:
import http.client import socket def get_it(url): try: # always set a timeout when you connect to an external server connection = http.client.HTTPSConnection(url, timeout=2) connection.request("GET", "/") response = connection.getresponse() return response.read() except socket.timeout: # in a real world scenario you would probably do stuff if the # socket goes into timeout pass urls = [ "www.google.com", "www.youtube.com", "www.wikipedia.org", "www.reddit.com", "www.httpbin.org" ] * 200 for url in urls: get_it(url)
(I wouldn't use the standard library as an HTTP client but for the purpose of this post it's okay)
As you can see there's no magic here. Python iterates on 1000 URLs and calls each of them.
This thing on my computer occupies 2% of the CPU and spends most of the time waiting for I/O:
$ time python io_bound_serial.py 20.67s user 5.37s system 855.03s real 24292kB mem
It runs for roughly 14 minutes. We can do better.Show me the trick!
from concurrent.futures import ThreadPoolExecutor as PoolExecutor import http.client import socket def get_it(url): try: # always set a timeout when you connect to an external server connection = http.client.HTTPSConnection(url, timeout=2) connection.request("GET", "/") response = connection.getresponse() return response.read() except socket.timeout: # in a real world scenario you would probably do stuff if the # socket goes into timeout pass urls = [ "www.google.com", "www.youtube.com", "www.wikipedia.org", "www.reddit.com", "www.httpbin.org" ] * 200 with PoolExecutor(max_workers=4) as executor: for _ in executor.map(get_it, urls): pass
Let's see what changed:
# import a new API to create a thread pool from concurrent.futures import ThreadPoolExecutor as PoolExecutor # create a thread pool of 4 threads with PoolExecutor(max_workers=4) as executor: # distribute the 1000 URLs among 4 threads in the pool # _ is the body of each page that I'm ignoring right now for _ in executor.map(get_it, urls): pass
So, 3 lines of code, we made a slow serial task into a concurrent one, taking little short of 5 minutes:
$ time python io_bound_threads.py 21.40s user 6.10s system 294.07s real 31784kB mem
We went from 855.03s to 294.07s, a 2.9x increase!Wait, there's more
The great thing about this new API is that you can substitute
from concurrent.futures import ThreadPoolExecutor as PoolExecutor
from concurrent.futures import ProcessPoolExecutor as PoolExecutor
to tell Python to use processes instead of threads. Out of curiosity, let's see what happens to the running time:
$ time python io_bound_processes.py 22.19s user 6.03s system 270.28s real 23324kB mem
20 seconds less than the threaded version, not much different. Keep in mind that these are unscientific experiments and I'm using the computer while these scripts run.Bonus content
My computer has 4 cores, let's see what happens to the threaded versions increasing the number of worker threads:
# 6 threads 20.48s user 5.19s system 155.92s real 35876kB mem # 8 threads 23.48s user 5.55s system 178.29s real 40472kB mem # 16 threads 23.77s user 5.44s system 119.69s real 58928kB mem # 32 threads 21.88s user 4.81s system 119.26s real 96136kB mem
Three things to notice: RAM occupation obviously increases, we hit a wall around 16 threads and at 16 threads we're more than 7x faster than the serial version.
If you don't recognize time's output is because I've aliased it like this:
time='gtime -f '\''%Us user %Ss system %es real %MkB mem -- %C'\'
where gtime is installed by brew install gnu-timeConclusions
I think ThreadPoolExecutor and ProcessPoolExecutor are super cool additions to Python's standard library. You could have done mostly everything they do with the "older" threading, multiprocessing and with FIFO queues but this API is so much better.
Python GUI Programming Projects using Tkinter and Python 3
Learn Hands-On Python Programming By Creating Projects, GUIs and Graphics
Python is a dynamic modern object -oriented programming language
It is easy to learn and can be used to do a lot of things both big and small
Python is what is referred to as a high level language
Python is used in the industry for things like embedded software, web development, desktop applications, and even mobile apps!
SQL-Lite allows your applications to become even more powerful by storing, retrieving, and filtering through large data sets easily
If you want to learn to code, Python GUIs are the best way to start!
I designed this programming course to be easily understood by absolute beginners and young people. We start with basic Python programming concepts. Reinforce the same by developing Project and GUIs.
The Python coding language integrates well with other platforms – and runs on virtually all modern devices. If you’re new to coding, you can easily learn the basics in this fast and powerful coding environment. If you have experience with other computer languages, you’ll find Python simple and straightforward. This OSI-approved open-source language allows free use and distribution – even commercial distribution.
When and how do I start a career as a Python programmer?
In an independent third party survey, it has been revealed that the Python programming language is currently the most popular language for data scientists worldwide. This claim is substantiated by the Institute of Electrical and Electronic Engineers, which tracks programming languages by popularity. According to them, Python is the second most popular programming language this year for development on the web after Java.
Python Job Profiles
The median total pay for Python jobs in California, United States is $74,410, for a professional with one year of experience
Below are graphs depicting average Python salary by city
The first chart depicts average salary for a Python professional with one year of experience and the second chart depicts the average salaries by years of experience
Who Uses Python?
This course gives you a solid set of skills in one of today’s top programming languages. Today’s biggest companies (and smartest startups) use Python, including Google, Facebook, Instagram, Amazon, IBM, and NASA. Python is increasingly being used for scientific computations and data analysis
Take this course today and learn the skills you need to rub shoulders with today’s tech industry giants. Have fun, create and control intriguing and interactive Python GUIs, and enjoy a bright future! Best of Luck
Who is the target audience?
Anyone who wants to learn to code
For Complete Programming Beginners
For People New to Python
This course was designed for students with little to no programming experience
People interested in building Projects
Anyone looking to start with Python GUI development
Access to a computer
Download Python (FREE)
Should have an interest in programming
Interest in learning Python programming
Install Python 3.6 on your computer
What will you learn
Build Python Graphical User Interfaces(GUI) with Tkinter
Be able to use the in-built Python modules for their own projects
Use programming fundamentals to build a calculator
Use advanced Python concepts to code
Build Your GUI in Python programming
Use programming fundamentals to build a Project
Signup Login & Registration Programs
Job Interview Preparation Questions
& Much More
Guide to Python Programming Language
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.
The course is begin continuously developing by adding lectures regularly.
Please see the Promo and free sample video to get to know more.
Hope you will enjoy it.
An Enthusiast Mind
Basic Knowledge To Use Computer
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language
Python Programming Tutorials For Beginners
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.
As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about
programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.
In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With
Lots of exercises and more fun stuff, let's get started.
Download free Exercise files.
Who is the target audience?
First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future