What do you focus on once you've learned the core concepts of the Python programming language
and ecosystem? Obviously, knowing a few fundamental packages in your space is critical.
So if you're a web developer, you should probably know Flask or Pyramid and SQLAlchemy really well.
If you're a data scientist, import pandas, numpy, matplotlib need to be something you type often
and intuitively. But then what? Well, I have a few topics for you. This week, you'll meet Mark
Summerfield, a prolific author of many Python books. We spend our time digging into the ideas
behind his book, Python in Practice, Create Better Programs Using Concurrency, Libraries,
and Patterns. What I really like about these topics is that they have a long shelf life.
You'll find them relevant over time, even as frameworks come and go.
This is Talk Python to Me, episode 58, recorded May 4th, 2016.
Welcome to Talk Python to Me, a weekly podcast on Python, the language, the libraries, the
ecosystem, and the personalities. This is your host, Michael Kennedy. Follow me on Twitter,
where I'm @mkennedy. Keep up with the show and listen to past episodes at talkpython.fm
and follow the show on Twitter via at Talk Python. This episode is brought to you by Hired and SnapCI.
Thank them for supporting the show on Twitter via at Hired underscore HQ and at Snap underscore CI.
Hey, everyone. A couple things to share with you. First, we'll be giving away an electronic copy of Mark's book this week.
As always, just be registered as a friend of the show on talkpython.fm. I'll pick a winner later in the week.
Next, I had the honor to spend an hour with Cecil Phillip and Richie Rump on their laid-back,
technical but casual podcast, Away from the Keyboard.
I really enjoyed the conversation, and I think you'll like their podcast, too.
Give them a listen at awayfromthekeyboard.com.
Finally, those of you taking my Python Jumpstart by Building 10 Apps course,
I have a few improvements for you.
I added transcripts to the player, the website, and the GitHub repository,
as well as added some activity tracking across devices so you know which lectures you've watched.
I hope you find these additions useful.
Now, let's chat with Mark.
Mark, welcome to the show.
Oh, thank you very much. I'm glad to be on it.
Yeah, I'm super excited to talk about all these great books you've written,
and one of them in particular really caught my attention, called Python in Practice,
Create Better Programs Using Concurrency, Libraries, and Patterns.
And that just really speaks to me on some of the most important parts of
sort of design pattern and improving your overall skill,
not just focused on libraries, like Flask or something.
Yeah, sure.
One of the motivations for writing that particular book was
I wanted to write something for people who were already comfortable writing Python,
but showing more of the high-level things you could do with Python.
You know, if you wanted, for example, to do really low-level networking with TCPIP,
you can do that in Python.
It's got the libraries.
All the facilities are there.
But if what you're more interested in is application programming,
you might not want to go so low-level.
And Python, either built-in or in third-party libraries,
has wonderful facilities for doing high-level stuff,
whether it's networking, concurrency, and GUIs, and things like that.
So I wanted to look at some of the facilities that are available,
both built-in and third-party,
that allow you to do some fantastic things with Python
without having to get right down into some nitty-gritty details.
Yeah, that's beautiful.
I find that when people are new to Python,
and this includes myself when I'm working in some area that I haven't worked in a lot,
I'll not realize there's some really simple thing that I can use.
And I think it's really great.
There's a lot of those little tips and tricks in your book.
But before we get into the details of your book,
let's start at the beginning.
Sure.
How did you get into programming in Python?
I started programming on paper in the late 70s.
I started reading computer magazines.
I taught myself basic purely off the magazines.
And I wrote my code on paper, and I executed it on paper.
Then eventually, I bought a home computer.
I don't know if your listeners will remember what they are,
but they were things before PCs.
Very limited, but quite a lot of fun.
And eventually, I went on to do a computer science degree, which I absolutely loved.
And that gave me a lot of the sort of theoretical background.
And then I just went into software development.
And I'd been doing that for quite a few years when I bumped into someone who suggested trying Python.
And I borrowed a book from a colleague, a fellow developer on Python.
And I hated the book.
And that put me off Python for about a year.
And that was one of the motivations for writing my first Python book,
was to write one that would actually encourage people to use it.
But once I started using it, within a year, all of my utility programs and tools that I use for my daily work,
they were in Python, because I just loved it.
So that was around 1999.
And now, everything I do, the first choice language is always Python.
Yeah, the Python ecosystem and, frankly, the language is fairly different from the late 90s today.
Oh, massively different.
I mean, I actually didn't like the indentation at first.
That took me like 48 hours before it really clicked.
Wow, no braces.
I just don't have to bother.
And that was really nice.
And also, of course, it forces your code to be quite neat in the first place.
It doesn't, of course, make you use good variable names and things like that.
You have to learn that separately.
But that applies to any language.
But I really liked Python pretty well from the get-go.
Yeah, so did I.
I think the indentation does catch a lot of people off guard.
And to me, it's kind of like good science fiction.
You have to sort of take this moment, the suspension of disbelief.
Like, just imagine for a minute this white space concept is a good idea and work with it for a week.
And then it just dawns on you, like, wow, this really is a good idea.
Like, I went back to working in some C-based languages right after I sort of learned Python and started working in it.
And the white space was a shock to me at first.
But what was even a bigger shock was these C-based languages that I loved.
I all of a sudden hated all the parentheses, the curly braces, the semicolons.
And that was such a surprise to me that I felt that way.
But it was within a week.
It was just completely over the semicolon.
Sure.
And of course, there's no dangling else problem that you can get in C or C++.
You know, if you've got an else with the correct indentation, you know what's going to be executed.
You're not going to get caught because you hadn't put in braces, you know?
Yeah, absolutely.
Yeah.
It really works for me.
And Python, the language.
Okay.
It's Turing complete.
And so is Perl.
And so is C++ and Java and all of these languages.
So you can do anything in one of them that you can do in the other.
So why choose one particular language rather than another?
And I think part of that is, well, what's the libraries and ecosystem like?
And part of it is what fits the way you think best.
And in my case, it happened to be Python that works.
But, you know, I wouldn't argue against someone who preferred some other language if that suited them.
Yeah, absolutely.
But for me, Python is a great language.
And particularly Python 3.
I started using that from the first alpha.
I'd ported everything from Python 2 to Python 3 right at that stage.
And I think it's excellent.
Yeah.
You know, maybe that's a good segue to sort of taking a survey of the books that you've written.
Because you've written many books.
Is something, how many, around eight, seven?
Depends whether you count second editions.
Without second editions, it's seven.
And with them, it's nine.
Okay, excellent.
Yeah.
And one of them you wrote is a pretty sizable
Python 3 book, right?
Yes.
That's Programming in Python 3.
Yeah.
That book is really aimed at people who can program in something or other.
And it's to port them over to Python 3.
But it should also work for people.
And the something or other could be Python 2.
Right, right.
So that's who it's aimed for.
It's not aimed at beginners.
The subtitle was poorly chosen by me.
A complete introduction to the Python language.
Sometimes people think that introduction means it's introductory, which wasn't the intended intention.
It was just, it's introducing everything that Python 3's got that you're going to use in normal, maintainable programming.
The only things I don't tend to cover in my books are things that I think are dangerous and obscure.
So, for example, in Python, you can disassemble Python bytecode, rewrite it, and put it back.
And that's brilliant.
But I wouldn't ever cover that in my books because I wanted to cover stuff that people can maintain.
Understand that it runs.
That's right.
Absolutely.
Maintainability and understandability are really important to me because, in my experience, you live with code for quite a long time.
You know, literally years.
So you don't want to torture yourself when you have to go back and fix something or do a modification to something.
And it's been a few years since you've seen that code.
Yeah.
Oh, absolutely.
So maybe tell us some of the other books that you wrote.
Okay.
You've got some interesting topics there.
I think the first one I wrote concerning Python is Rapid GUI Programming with Python and Qt, which is about PyCute programming with PyCute 4.
Although the book is – I mean, I've updated the examples for PySide as well.
I really like the Qt GUI toolkit.
I like cross-platform GUI programming.
And that book, the first part of it is actually a very brief introduction to Python programming itself.
And I was quite pleased.
I had very good feedback on that.
Many people said, well, I already knew Python, but I read the introduction because, well, I bought the book.
And I still learned things from it.
So I was glad about that.
And Qt itself, I think, is good.
I know that it's very fashionable, you know, mobile programming and web programming and things like that.
And they're great.
But I personally love desktop programming and desktop GUI applications.
So it was an expression of something that I was really interested in and really enjoyed doing.
Yeah, that's great.
I agree with you that desktop apps are really – they don't get enough love, you know, because mobile is a super flashy thing.
I actually spend most of my time writing web apps.
But I do very much appreciate a good desktop app.
And I think Qt is one of those frameworks that is cross-platform but doesn't feel cross-platform as a user.
You're not like, oh, yeah, this button and this UI completely looks foreign.
But it technically is a UI, right?
Absolutely.
Yeah.
I mean, it really does – they really do well with native look and feel, even though they're not using native widgets, unlike, say, WXPython, which does.
They emulate it all.
But they do it very well.
And some of their stuff on Windows is faster than native.
Wow.
The way that it's implemented.
Because they're not – when they create a window, they're not creating all those widgets, which you would do using a normal Windows toolkit.
They just basically create an image.
Right, right.
So it's much, much cheaper and much faster in terms of resources.
So, yeah, I love that toolkit.
And that's how I'm earning my living now because I'm earning my living writing on the back of commercial Python applications for desktop users.
So they're written in Python, Python 3, PySidem Qt.
Oh, wonderful.
And do you ship those with something like CX Freeze to sort of package them up or was it PyToApp?
That's exactly right.
No, I use CX Freeze.
And so, yeah, they're quite a big download because the Qt libraries are all in the bundle.
And people can download them, try them, and, you know, hopefully buy them.
Yeah, wonderful.
And that seems to have worked for the last couple of years quite nicely.
Yeah, okay.
Fantastic.
Yeah, that's a really cool story.
It is possible to earn a living with Python not doing web programming.
I mean, obviously, you can earn good money doing web programming as well.
Right, of course.
Or data science.
Those are the two most common ones.
Oh, absolutely.
Yeah.
Yeah, absolutely.
So you also have some books on Go and C++.
Oh, I've got a Go book.
I did that because I was just really interested in Go.
I think Go and Rust are both sort of new languages that I am interested in.
And I like the simplicity of Go.
And I'm also interested in concurrency.
And so I started to learn the language.
And I thought, yeah, I can write something better than what's available on this language.
But one of the authors of Go has come out with a book on Go now.
So I should think that will kill mine.
You never know.
I mean, well, I still think mine's a good book, actually.
But there we are.
In terms of C++, I have written a few.
But they're all C++ with Q.
So I co-wrote with Jasmine Blanchett, C++ GUI programming with Q3 and then with Q4.
And then I did a solo one, advanced Q programming.
Okay.
But again, they're all C++.
I still use them for PiSide programming, you know, to remind myself how to do things.
And I actually use the C++ docs rather than the PiSide docs.
So, you know, I can translate easily enough.
Yeah, they're a little more definitive, maybe.
Yeah.
I am frustrated with C++.
I mean, C++11 was like a huge step forward.
But they just didn't deprecate enough as far as I'm concerned.
So it's getting too big.
And I think it's quite hard to write that language in a maintainable way now.
Yeah, that language is just ever-growing.
And it wasn't all that simple in the beginning.
No.
I have another question about Qt.
I definitely want to get to the topics of your books.
Sure.
Given your background there.
I just saw, when was this, back in mid-April, there was an announcement saying we're bringing
PiSide back to the Qt project.
That's right.
I'm really delighted.
Fantastic.
It's going to be PiSide for Qt 5.
Now, you can use PiQt with Qt 5.
But PiSide has been Qt 4 only so far.
But they're actually putting money behind it and investing in it.
So PiSide 2 will be for Qt 5.
And I'm really looking forward to that.
Yeah.
So am I.
Do you know the time frame on when that kind of stuff will be out?
They're doing the development in the open.
I think there's like a GitHub or some equivalent of that.
So you can look at it.
But I would guess it's going to be, I think we'd be lucky to see by the end of the year
something that's usable.
Right.
Because it's not a simple job.
Yeah.
There's a pretty big break from Qt 4 to Qt 5.
Not so much the Qt side of it.
I don't think that's the hard side.
As you know, programmers, we love reinventing things.
And when they did PiSide, they invented a new way of doing bindings.
And I think that hasn't proven to be quite as maintainable and flexible as they'd hoped.
So I think that's where they're going to have to do quite a lot of work, getting that to
work with Qt 5 and the new C++.
Right.
Okay.
Yeah.
So it's more with the PiSide version than it is the Qt itself.
Got it.
I think so.
Yeah.
All right.
Excellent.
So I know there's a lot of interest in GUI apps from a Python perspective.
And maybe another time we can dig into Qt even more.
But let's talk a little bit about creating better Python apps.
Okay.
What was the motivation for writing this book?
I mean, you said you aimed it at people who were in the middle, but you gave it four themes.
You said, I'm going to cover sort of these general themes of code elegance, improving speed
with concurrency, networking and graphics.
How did you come to that collection?
Well, graphics, because I just love GUI programming.
So that had to go in because it's something I love.
I also, with networking, I've done a fair bit of network programming, but I'm not a low-level
person.
I like my networking to be as easy as possible.
And I wanted people to be aware that you can do networking really easily with Python without
having to go down, you know, to low-level stuff and do it in a reliable and pleasant way.
And I cover two approaches.
One is XMLRPC.
And I cover that because it's built, it's in the standard library.
And it works really easily.
So it's really nice.
And the other one I cover is a third-party one called RPYC.
There's another one called Pyro, which is also widely used.
And I could have gone with either of those.
And the advantage of RPYC or Pyro is that they can be Python-specific.
So you can get better performance, whereas XMLRPC is general.
So it's not got quite as good the performance, but it has the advantage that you can write a
client and or server using XMLRPC.
And it'll talk to anything else that uses the XMLRPC protocol.
So that's very nice for interoperability.
And they're high-level.
So all of the detail and, you know, timeouts and all of the issues that can arise in networking
is just neatly controlled and wrapped up.
And, OK, you get exceptions and things like that.
You know, all normal Python stuff.
So you don't have to worry about the details.
Yeah, it makes a lot of sense.
So I want people to be aware of that, that these kind of facilities exist.
You can basically write a wide range of different types of networking apps in Python, right?
You can go all the way down to the raw sockets with Byte.
I just talked with Mahmoud Hashemi from PayPal.
And those guys are writing services that take over a billion requests a day.
Wow.
They're doing network programming in Python, but down below the HTTP level.
Wow.
And these custom APIs.
And then you can, of course, go up higher, right?
Like XML RPC or maybe REST service with requests, things like that, right?
Absolutely.
And it's the high level stuff that I was more interested in.
And I think that's because at heart, I'm an applications programmer.
And that means I know about the subjects of my application, but I don't necessarily have the
expertise in particular areas that the application needs.
And so for that, I want high level libraries that give me the functionality that are created
by experts in those fields.
So I get the best of both worlds.
I get the functionality I need by, you know, excellent people who've developed it without
actually having to learn all that stuff myself.
Yeah, absolutely.
I think the right way to start is start simple and then, you know, then go do crazy network
stuff if you need to improve the performance.
But generally, you don't have a performance problem.
No, no.
And that actually brings us nicely to concurrency.
Python, you know, people say, oh, is Python slower?
Python can't do concurrency.
And I really wanted to address those issues because how slow is Python really?
Well, I developed a program in C++ that was very CPU intensive.
And I rewrote that program in Python and it was 50% slower.
And I think that's not bad going from C++ to an interpreted language, a bytecode interpreted
language.
But of course, I then made it concurrent.
And you could make it concurrent in C++, but it's much easier.
They're doing that in Python.
And so on a dual core machine, suddenly it was as fast as C++.
Give it more cores.
And it was faster than the C++.
So even though baseline, yeah, it's 50% slower on real hardware using concurrency, it was much
faster.
And that's really what the user is going to care about.
You know, on my hardware is this thing running fast.
Right.
And of course, it's much more maintainable.
I mean, doing concurrency in Python is so much easier than in most other languages.
Yeah.
And I think specifically around concurrency, it's easy to get yourself into a situation where
you've been very clever and you've thought really hard about the algorithms and the way
it works.
And you've kind of written something just at the limit of your understanding.
Like you totally understood what you did, but it's at the very edge.
But of course, understanding multithreaded code, debugging it is harder than writing it.
And so maybe it's like, you know, you've sort of gone a little too far.
You're like, okay, I could write this, but I don't really understand how to fix it when it
goes wrong.
Yeah.
And that is a real problem with concurrency.
In some languages, you're stuck because the concurrency facilities, they offer pretty basic.
So they don't make it easy.
But Python offers higher level concurrent approaches to concurrency as well as low level.
For example, you've got the concurrent futures module, which makes it much easier to create
either separate threads or separate processes where Python takes care of lots of the low level
details.
Within the first week, and there are no obligations ever.
Sounds awesome, doesn't it?
Well, did I mention the signing bonus?
Everyone who accepts a job from Hired gets a $1,000 signing bonus.
And as Talk Python listeners, it gets way sweeter.
Use the link Hired.com slash Talk Python to me and Hired will double the signing bonus to $2,000.
Opportunity's knocking.
Visit Hired.com slash Talk Python to me and answer the call.
I think you really put this together quite nicely in terms of sort of breaking out the different
types of concurrency.
And it helps you understand if you sort of think, okay, well, what are the types of concurrency?
What type of problem am I solving?
Then you have a pretty good recommendation for if it's this type of problem, solve it this
way.
And so you said there were three types.
You called them threaded concurrency, process-based concurrency, and then concurrent weighting.
Yeah, yeah.
Basically, if you're doing CPU-intensive work, then using threading in Python is not going to
help because of the global interpreter lock.
So if it's CPU-intensive and you need concurrency, then you need to use a different method.
And Python offers, for example, multi-processing, where you can split your work over multiple
processes rather than multiple threads.
And each of those has its own separate interpreter lock so that they don't interfere with each
other.
Right.
But I think the key to getting real performance from concurrency is to avoid sharing insofar as
you can.
And that means either you don't need to share in the first place, or if you've got data that
needs to be looked at by your multiple threads or multiple processes, then it may be cheaper
to copy that data rather than have them share some kind of locking to look at it.
Right.
And that's a mistake I think a lot of people make is they see the way that their program is
working now.
They've got some shared...
They've got a pointer they're passing to two methods or whatever, two parts of the
algorithm.
And they're saying, well, this part's going to work on this part of the memory and this
one's going to work over here.
And so they think, well, when I parallelize this, this is shared memory access.
And of course, you have to take a lock or somehow serialize access to that data.
And it's easy to forget that, you know, if that's like a meg or even maybe 50 megs of
data, it might be so much easier both for you and advantageous for performance to just say
copy.
Make a copy and just pass it over and then correlate the differences later.
Absolutely.
The other possibility is to share the data without copying, which is fine if you never write to
it.
So if you've got data where you're just reading in information like a log or some data
stream, and you're never changing the information you're reading, you might be producing new output,
but that's separate.
So if the stuff you're reading, you can read that from a shared data structure as long as
you read.
And that's not going to be a problem.
It's only when you're going to start writing that sharing becomes an issue.
And then you've got problems if you don't lock.
But the best way is still don't lock.
The best way if you're writing data is write or save the data in separate chunks and gather
it together at the end.
That will often be less error prone and faster.
Yeah, absolutely.
But of course, sometimes there isn't a choice.
Sometimes you do need to lock.
And then that's when it becomes quite difficult to reason about because you've got to be clear
when you need to lock and when you need to unlock and all the rest of it.
And that's where the difficulty comes in.
But if you can avoid having to lock, then you can get good performance without problems.
Absolutely.
And there's some of the data structures in the newer versions of Python that sort of solve
that problem for you.
And so we'll talk those a little bit.
But when you said copy data, one way to say copy a data structure, like obviously you can't
just pass the pointer over or get another variable and point to the same thing, right?
Because it's a pass by reference, not pass by value type of semantics.
So there's actually a copy module in the standard library, right?
Yeah.
And basically, what I would contend is this.
If you use copy.deepcopy in a non-concurrent program, then there's almost certainly something
wrong with your logic.
But if you're using a concurrent programming, then it may well be the right solution for you.
Because deep copying can be expensive if you're using like nested data structures, like dictionaries
with dictionaries and things that are quite large, for example.
But nonetheless, it may be the right tradeoff.
Of course, the only way you're going to know for sure is to profile and actually time things.
Because that's the other sort of big issue, isn't it?
That, you know, we have an intuitive feeling this will be fast or that will be slow.
But unless you back it up with numbers, you could be optimizing something that makes no
difference whatsoever.
Yeah, you had some really interesting points there that I thought were both interesting and
good advice.
One was, if you're going to write a concurrent program, write the non-concurrent serial single
threaded version.
Absolutely.
first, if you can, and then use that as the baseline for your future work, right?
I mean, one of my commercial programs, it does its work using concurrency.
It's written in Python and it's concurrent.
But I have two modules that do the processing.
And one uses concurrency and one doesn't.
And the tests, I have to make sure they produce exactly the same results.
And of course, one is much slower than the other.
But I found that incredibly useful in the early days, particularly for debugging.
And I still use the non-concurrent one if there's some tricky area that I want to focus
on without having to worry about concurrency.
So I found it's paid off in terms of saving my time as a programmer.
And that's the other kind of time, isn't it?
It's not just the processing or runtime of your software.
It's the time you spend not just creating this stuff, but maintaining it.
And concurrency can cost you a lot of maintenance.
Oh, yes.
Unless you're very careful.
Yeah, I'm a strong advocate.
Get the non-concurrent version working first.
And of course, it may turn out that that's actually fast enough anyway.
Absolutely.
And then you've saved yourself a whole lot of trouble.
It's sort of the whole premature optimization issues.
Yeah.
It's easy to get seduced into doing things concurrently because it's very fashionable and you can brag
about it.
But quite honestly, it's got to be the right solution.
And you're not going to know that until you've done a non-concurrent one first, I think.
Yeah, that's a good point.
And sort of related to that is the performance story.
So you have some examples in your book where you write the concurrent version and then you have
the concurrent version, but you write it in several ways.
And you also have the serial version.
That's right.
Just to compare to see, to show what difference different strategies make depending on circumstances.
Right.
And for the CPU based one, you were doing like image analysis and processing.
Yeah, because that's expensive in terms of CPU.
And of course, if you use threading, it kills performance.
Yeah.
Ironically, in CPython, it's actually several times slower if you do it in parallel.
Yeah, because it's only actually running on a single call at a time and you've got context
switching on top of it.
Whereas if you use multi-processing, Zoom, it can run free.
You know, it'll max out your calls and it will go as fast as your machine's capable of.
The performance story around threading is super hard to see the whole picture because we obviously
know when you take a lock here, that slows down both the threads and the context switching
is slow.
But then you also have the sort of memory usage.
You have the L1, L2 caches and registers.
And so like when you switch from one thread to the other, it could pull in data that trashes
your cache and you've got to go get it from main memory, which is like 100 times slower.
And it's just, it's very subtle.
And yeah, if you're networking, then generally using threads is fine because the network latency
is so dominant.
I'm not talking about in terms of if you're like Google and doing like massive servers,
but for a lot of, if you like more ordinary applications, then threading is fine for that.
But of course there is the new asyncio library.
The GIL is basically one of the problems.
It means you don't get any of the concurrency that you're aiming for computationally, but
you still get all the overhead.
Well, you don't get it at the Python level.
You will get it at the C level.
So if you have something that's running with Python threads and actually the work is being
done by say a C library, if that C library is written well, it'll release the GIL, do its
work and then reacquire it when it needs to pass data back.
So it's, it's, it's not a simple story, no matter how you look at it.
Yeah.
Okay.
So you still have, you've got a performance test, but you've got to have that serial version
to give you a benchmark so that you know whether you're getting better or worse.
Right.
Absolutely.
And also just doing that serial one, it will give you insights into the problem you're solving
anyway.
And it's better to make mistakes with the serial one than with the concurrent one because you've
got less to think about then.
Yeah, that's for sure.
If you really do have a problem and it is slower, the trick is to use sub processes.
Yeah, use the multi-processing module, which prior to 3.2, I found not terribly reliable,
but they did loads of improvements in 3.2 and certainly in 3.3 and 4.
It's absolutely rock solid, both Windows and Linux, which are the ones, the platforms I use.
And it's brilliant.
It's absolutely an excellent library.
Yeah.
And I just want to point out to people, like when you hear say use sub processes, that doesn't
mean just go kick off a bunch of processes and manage it yourself, right?
There's sort of a concurrent library for managing them, right?
Multi-processing library.
Oh, the multi- yeah.
I mean, there's nothing to stop you.
I mean, there is the sub process module.
You can do it all manually, but there's no reason to do that.
I mean, you can create a process pool.
In one of my applications, I do that.
And there's an asynchronous function.
You can basically just give it a Python function and some arguments and say, okay, go do this
somewhere else on some other processor.
And it'll just do that work.
If that's like expensive work, that's great because it doesn't stop your main process at
all.
And when it's done the work, it lets you know.
And you can pick up from there.
Right.
So there's the concurrent futures module, which can be, which is a very high level module,
which makes it really easy to just execute either with threads or processes stuff.
Or you can go use the multi-processing module itself with its pools and stuff.
So you can find the level that suits what you need.
Yeah.
It feels to me like if you're doing Python 3.2 or above, you should really consider maybe
the concurrent module first and the concurrent futures, because it's so easy to say, let's
do this computationally.
Let's do this.
Yeah.
Let's do this as sub processes.
Let's switch it to have like a pool of sub processes.
All of those things.
Right.
Yeah.
The other thing about multi-processing is by default, it doesn't share memory, which is
the opposite of threading.
Which means you're not going to clobber yourself by writing to something that's shared.
Of course, it means if you do want to share, you have to actually go to extra effort and
say, OK, I'm setting up this thing to be shared.
Continuous delivery isn't just a buzzword.
It's a shift in productivity that will help your whole team become more efficient.
With SnapCI's continuous delivery tool, you can test, debug, and deploy your code quickly
and reliably.
Get your product in the hands of your users faster and deploy from just about anywhere at
any time.
And did you know that ThoughtWorks literally wrote the book on continuous integration and
continuous delivery?
Connect Snap to your GitHub repo and they'll build and run your first pipeline automagically.
Thanks SnapCI for sponsoring this episode by trying them for free at snap.ci slash talkpython.
Yeah, there's some built-in data structures for sharing across process, right?
There are.
I mean, I only use them personally for flags.
You know, I tend to gather my data separately if I've got results data and then join it all
up together at the end.
So I just use flags basically to say, like, this bit's at this stage or a flag to say, look,
just stop now because the user's cancelled.
Yeah.
To give me a clean termination.
But, you know, I mean, I cover all that sort of stuff in the book.
But it's a really great module, multiprocessing.
But concurrent futures gives you that high-level approach, which makes it as simple as it can
be for this kind of stuff.
I mean, I'd still advocate not using concurrency unless you need it, you know, because it does
make your program more complicated and harder to reason with, you know, or reason about.
Yeah.
And, you know, it is easy to switch between concurrent futures using subprocesses or using
threads.
But that doesn't mean the code that you write can be just flipped from one to the other because
of the serialization issues and all, you know, the locking shared data.
So that's maybe a really subtle thing you could run into.
Well, if you're doing threading, the memory is shared by default.
So any thread can stomp on anything, which can be a problem.
But on the other hand, if you're using multiprocessing, any data that you're passing around has to
be picklable, for example, which doesn't apply as a limit in threading because you're just
accessing the same data in the same memory space.
So there are differences and there are tradeoffs.
And the API for multiprocessing started as mimicking the threading API, but it's actually
grown considerably since then.
So it's worth digging in and learning.
But I start with the concurrent futures because that is the easiest conceptually and impractical
code.
It requires the least code to get stuff done.
Yeah, absolutely.
So both the multiprocessing and the threading are pretty good for when you're doing basic
IO bound work, right?
Because the key thing to know about that is a thread when it waits on a network call in
CPython will release the gill, right?
Yeah.
But of course, there is the asyncio module, which is designed for that kind of work.
I'm not a user of that module because most of my processing is CPU bound.
Right.
But that is a third way, if you like.
Yeah.
So in Python 3.4, they added the asyncio and the concept of event loops.
And I also have not used that a lot.
But my understanding is that's a little like the Node.js style of programming.
I don't know because I avoid JavaScript as much as I can.
But basically waiting on the IO bits and releasing it to process other bits of code, other methods
while you're waiting on IO, right?
So it's going to let you know rather than you having to poll or be blocked.
Right.
Very callback driven.
Yeah.
Which is a perfectly good approach.
Yeah.
And then Python 3.5 added the async and await keywords.
Yeah.
Which I haven't used.
I'm still using 3.4.
Partly because I had some compatibility issues with CX Freeze at the time.
And partly because of the installer.
For my commercial software, I released both 32 and 64 bit versions on Windows.
And up to 3.4, it's really easy to install both of those side by side.
There's no problem.
But with a 3.5 installer, what I found was that some third party libraries couldn't find one
or the other.
So I'm a bit stuck with 3.5 on Windows at the moment.
Well, and the installer for Python 3.5 got a major reworking by Steve Dower, who was actually
just on the show.
What number was that?
That was 53.
So just, you know, a few weeks ago.
And the installer is much nicer than the old one.
It is, but it doesn't do what I want.
But it has, you know, right?
If you need this other thing it's not doing, then obviously you can't use it, right?
Yeah.
I need to be able to install 32 and 64 bit Python side by side.
And I can do that up to 3.4.
I'm not saying it's not possible.
I mean, I have done it with 3.5.
But what I haven't managed to do is get my third party stuff.
PyWin32 and APSW, which I'll mention at the end.
I couldn't get them working properly with it when I had both.
They work fine when I've just got one Python, but not when I had both.
But hopefully that problem will go away.
Because, I mean, sometime I'm going to, like, stop doing 32-bit versions of my apps.
I really want to look into the async and await stuff more.
Because that programming model is so beautiful.
It's just I haven't been writing any code that requires that type of work.
I like that model because it's very similar to the GUI event loop model.
I mean, the GUI event loop basically sits there and says, I'll let you know if something happens.
And you say, okay, well, if this thing happens, call this.
Yeah.
GUIs are inherently event-driven, right?
Absolutely.
They've got their message pump and everything.
So you actually, one of the, maybe the last section in the concurrency bit of your book, you talk about special considerations for GUIs.
Yeah.
I mean, I did this using TKinter simply because that's in the box.
You know, comes with Python out of the box.
Although, personally, I use PySide and Q.
But it would work.
The method works with both.
And I'm sure it would work with WX or with PyG objects, any GUI system.
And what I discovered was, how do you make, well, the question that arose for me was, okay, I've got a GUI application.
And it's got to do some CPU intensive work.
But I don't want to freeze the CPU.
Because what if the user wants to cancel the operation?
Or what if they just want to quit the application?
I don't want it frozen for like, you know, minutes on end when they can't do anything.
One of the quickest ways you can make a user believe that your application is crappy is to have it just lock up.
And on Windows, like, get that sort of white, opaque overlay saying not responding.
Or on OS X, it says force quit.
You're like, hmm, I'm a little suspicious now.
Yeah, and sometimes those messages come too early because sometimes, you know.
But anyway, so that was the problem that I had to address.
And what I found was, if I use threading, I do have a work thread and a GUI thread, the GUI still freezes.
So what I needed was some way of not having the GUI freeze.
And the model that I came up with was I have two threads, one for the GUI, and what I call, rather sarcastically, the manager thread.
And the thing about the manager thread is the GUI gives it work.
Whenever there's work to be done that's like CPU intensive, it gives it to the manager.
But like a good manager, the manager never does any work.
And that means that the GUI thread always gets all the CPU of its core, so it's never blocked.
And the manager's given all the work and never does any work.
And that solved the problem because what the manager does, it uses multiprocessing to hand it off to other processes.
And if you've got multiple cores, that's no problem.
I did try it on single core machines, and it was still no problem.
Right, because you still have the preemptive multithreading that gives you enough time slice that your user feels like your app's working.
So basically, you've got two threads.
The GUI thread gets all the CPU for its core.
And whenever it has work, it gives it to the manager who immediately hands it on to a process in a process pool.
And that process is separate and goes off and does it and lets you know.
And, of course, is cancelable if the user wants that.
Right. Okay. Very, very nice.
Because it basically, it shares one int.
And the int is either going to say, like, you're good to go or, like, they don't want you anymore.
Stop work.
That model I cover in the book.
And as I say, it'll work with any, although I show it with TK Enter, it's not TK Enter specific.
Yeah, absolutely.
I think that's a pretty good summation of the concurrency story.
The other part of performance that you talked about that I actually don't know very much about and I haven't talked a lot about on my show is using Cython to speed up your code.
Can you tell everyone what Cython is and give a quick summary there?
Okay.
Cython is basically, it's a kind of compiler.
So, if you have an application written in pure, ordinary Python and you run it through Cython, it will create a C version of your code.
And my experience is that will basically run twice as fast, just without touching it, without doing anything, just because it's now C.
But you can then go further.
You can actually give it hints and say, well, you can give it type hints.
So, basically, you can say, well, this is an int or this is a string.
And if you give it hints, it can optimize it better.
It's also got optimizations for NumPy.
So, for people who are interested in that kind of processing.
So, it can produce very fast code for that.
Yeah, that's interesting.
And it's, I think, worth pointing out that it's not the same concept as type hints in Python 3.5, which is just more of an IDE tool, right?
That's right.
Typing module 4.3.5 is, in a sense, it has no functionality at runtime.
It is purely used for static code analysis to say, you know, whether you're being consistent with your types.
In other words, you're saying, I'm claiming that this is a list of strings and it will statically analyze your code and say, well, okay, you've only used it as if it were a list of strings.
So, that's good.
But, of course, a compiler could use that type hinting information to produce more optimized code.
And I expect that's where things will go.
Yeah, absolutely.
I'm hoping that Cython will actually adopt that syntax.
And that, I mean, there are other compilers like Nuitka and so on may adopt that.
Now that typing is a standard module, one hopes that the third-party compilers will adopt it.
Yeah, at least there's an option, right?
Yeah.
And it would mean consistent code then.
It would mean you could write your code using typing and know that whichever one of the compilers you chose would give you some kind of speed up.
Yeah, yeah.
Beautiful.
All right, so we don't have a lot of time left in the show, but I wanted to give you a chance to just talk about some of the projects on your website.
One of the ones that you were working on is something called Diff PDF.
Another one was the Gravitate Game.
I thought those were kind of interesting.
Oh, yeah.
The game was just a bit of fun.
I did it in one of my books.
I think it's actually in Python practice because I'd never put a game in a book.
And I thought, well, why not?
I wrote it in TK Inter, but I have got cute versions.
And on the website, I've got a JavaScript version that I did using the canvas, you know, the HTML5 canvas.
It's basically the same game or tile fall, but with the things gravitating to the middle rather than falling to the bottom and left.
That's it.
And yeah, you can do fun games with Python, no problem.
And of course, there is a Pi game library as well for people who are more sort of heavily into games.
Yeah.
Diff PDF is paying my salary.
Basically, it compares PDFs and you might think, well, that's easy.
You just compare the pixels and it can do that.
And lots of other tools can do that.
But what turns out to be quite tricky is comparing the text as text because PDFs are really a graphical file format.
So a PDF file doesn't actually know what a sentence is or even a word.
So it can break up text in quite weird ways.
And this PDF gives you a sort of rational comparison.
Yeah, cool.
So you can ask questions like, is the essential content changed, not just like something bolded or whatever, right?
Yeah.
And I thought it'd be used by publishers.
I wanted to use it originally to compare.
If you do a second printing of a book, not a second edition, a second printing, the publisher will let you make minor corrections as long as it doesn't change the pagination.
And having to like check that I hadn't messed up by looking at 300 or 400 or 500 pages was pretty tiring.
So that was an incentive for creating this tool.
You're like, within the time it would take me to do this, I could write an app and solve this.
Exactly.
But it turns out it's used by finance companies, insurance companies and banks.
Ah, the lawyer types, yeah.
Well, yeah, but why?
And I don't know because they won't tell me.
But they use it.
And as long as they buy, I don't care.
I mean, that's great.
That's cool.
And is that written in Python?
It is.
It was originally written in C++, but now it's written in Python.
It uses concurrency, the model of concurrency I described.
And it's a Windows-specific product.
It uses a third-party PDF library that I bought, a royalty-free library.
And yeah, that's been paying the way.
But I've come up with another program, one that I originally wanted to write more than 20 years ago.
But I didn't have the skill then, and the tools weren't available anyway.
And that's X-Index.
And it's for book indexes.
And there are a couple of – well, there are some existing products out there for book indexes.
But this one uses Python, and it uses SQLite, which I adore as a database.
I really like it.
Yeah, I'm a fan of it too.
Yeah.
So I get all the reliability and also the convenience.
I mean, SQLite has full-text search, which I think people from Google added to it.
And that's just absolutely superb.
Oh, yeah.
That's excellent.
So that application has only gone on sale the end of last month.
Wow.
Congratulations on that.
That's excellent.
Thank you.
So I'm really pleased about that.
And I'm waiting to see – like people can use it for 40 days free trial now.
So I'll see in a couple of months if people actually buy it.
Well, good luck on that.
That's great.
Yeah.
So before we end the show, let me ask you a couple of questions that I always ask everyone.
Yeah.
So there's close to 80,000 PyPI packages out there.
And everybody uses some that are super interesting that maybe don't get the rounds that everyone knows about.
So what are ones that you really like?
Well, obviously, I use the PySite.
I use the Roman one as well because I use Roman numerals like in the indexing thing.
Nice.
Yeah, I know.
But the – and obviously, I use CX3s.
And I use PyWin32, which is very useful for Windows.
And I use – and another Windows one I use is WMI, which is Windows –
Windows management infrastructure, I think.
Thank you very much because I'd forgotten.
But the one that I want to sort of boost, if you like, is APSW, another Python SQLite wrapper.
And as you know, the Python standard library has a SQLite 3 module, which is perfectly good.
Nothing wrong with that.
But APSW is absolutely excellent.
It provides you as a Python programmer with all the access to SQLite that you would get if you were a C programmer, but with all the pleasure of programming in Python.
Oh, wonderful.
So you can create your own custom functions in Python that you can feed into it.
So you can create your own collations.
You can even create your own virtual tables.
Everything that you can do in C, you can do in Python.
And it's just a fantastic library.
It doesn't follow precisely the DBAPI 2 standard.
It does where it can, but it favors – if there's a choice and like SQLite offers more, it offers you the more.
Because it's designed to give you everything that SQLite has to offer.
I mean, if you're wanting to prototype on SQLite for transferring to another database, then use the built-in SQLite 3.
But if you want to use SQLite in its own right, for example, as a file format or for some other purpose where you're only going to be using SQLite, then APSW is the best module I've ever seen for doing that.
Yeah, that's wonderful.
While we're on database packages, I'll also throw out records by Kenneth Wright, which he called SQL for humans, which is like the simplest possible sort of alternative to DBAPI that I can find.
It's like the opposite end.
It's like super simple, not like, you know, we're going to give you access to everything.
Okay.
I mean, I like writing raw SQL, so APSW suits me for that as well.
I mean, I know there are things like SQLAlchemy and, you know, that give you a higher level, but I love APSW.
Yeah, wonderful.
Wonderful.
Okay.
Last question.
When you write some Python code, what editor do you use?
I use gvim.
gvim.
Okay.
So graphical vim.
Now, I'm not saying that I'd recommend that.
I think that it could drive, you know, someone insane trying to learn it because it is a strange editor, but I've been using it for more than 20 years now.
I just, I would find it hard to use another one for daily work.
All right.
Excellent.
Well, thanks for the recommendation.
Okay.
And thank you very much for having me on your show.
Yeah, Mark.
It's been a great conversation.
And I'm really happy to shine a light on this whole concurrency story and the GUI story as well in Python because they don't get as much coverage as I think they should.
No.
And Python is good at both of those things.
You know, I don't believe people who say otherwise.
Python needs, gives you a lot of concurrency options, so you've got more choice and you need to choose with more care.
But if you choose well, then it will give you great performance.
Yeah, absolutely.
All right.
Well, thanks for being on the show.
It was great to talk to you.
Thank you very much.
This has been another episode of Talk Python to Me.
Today's guest was Mark Summerfield, and this episode has been sponsored by Hired and SnapCI.
Thank you both for supporting the show.
Hired wants to help you find your next big thing.
Visit Hired.com slash Talk Python to Me to get five or more offers with salary and equity presented right up front and a special listener signing bonus of $2,000.
SnapCI is modern, continuous integration and delivery.
Build, test, and deploy your code directly from GitHub, all in your browser with debugging,
Docker and parallels included.
Try them for free at snap.ci slash Talk Python.
Are you or a colleague trying to learn Python?
Have you tried books and videos that left you bored by just covering topics point by point?
Well, check out my online course, Python Jumpstart by Building 10 Apps at talkpython.fm/course to experience a more engaging way to learn Python.
You can find the links from today's show at talkpython.fm/episodes slash show slash 58.
Be sure to subscribe to the show.
Open your favorite podcatcher and search for Python.
We should be right at the top.
You can also find the iTunes feed at /itunes, Google Play feed at /play, and direct RSS feed at /rss on talkpython.fm.
Our theme music is Developers, Developers, Developers by Corey Smith, who goes by Smix.
You can hear the entire song at talkpython.fm/music.
This is your host, Michael Kennedy.
Thanks so much for listening.
I really appreciate it.
Smix, let's get out of here.
Staying with my voice.
There's no norm that I can feel within.
Haven't been sleeping.
I've been using lots of rest.
I'll pass the mic back to who rocked it best.