Latest / Talk Python To Me / #306: Scaling Python and Jupyter with ZeroMQ
Transcript
- 0:00When we talk about scaling software, threading and async get all the buzz.
- 0:04And while they are powerful, using asynchronous queues can often be much more effective.
- 0:08You might think this means creating a Celery server and maybe running RabbitMQ or Redis as well.
- 0:14What if you wanted this async ability and many more message exchange patterns like PubSub,
- 0:19but you wanted to do zero of that server work, none of it?
- 0:22Then you should check out ZeroMQ.
- 0:25ZeroMQ is to queuing what Flask is to web apps,
- 0:28a powerful and simple framework for you to build just what you need.
- 0:31You're almost certain to learn some new networking patterns and capabilities in this episode with our guest,
- 0:37Min Ragan-Kelley.
- 0:38He's here to discuss ZeroMQ for Python,
- 0:41as well as how ZeroMQ is central to the internals of Jupyter Notebooks.
- 0:45This is Talk Python To Me, episode 306, recorded February 11th, 2021.
- 0:50Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem,
- 1:08and the personalities.
- 1:09This is your host, Michael Kennedy.
- 1:11Follow me on Twitter where I'm @mkennedy,
- 1:14and keep up with the show and listen to past episodes at talkpython.fm,
- 1:17and follow the show on Twitter via at Talk Python.
- 1:21This episode is brought to you by Linode and Mido.
- 1:24If you want to host some Python in the cloud,
- 1:26check out Linode and use our code to get $100 credit.
- 1:29If you've ever wanted to work with Python's data science tools like Jupyter Notebooks and Pandas,
- 1:34but you wanted to do it like you worked with Excel,
- 1:36with a spreadsheet and a visual designer,
- 1:39check out Mido.
- 1:39One quick announcement before we get to the interview.
- 1:43We'll be giving away five tickets to attend PyCon US 2021.
- 1:47This conference is one of the primary sources of funding for the PSF,
- 1:51and it's going to be held May 14th to 15th online.
- 1:54And because it's online this year, it's open to anyone around the world.
- 1:58So we decided to run a contest to help people,
- 2:00especially those who have never been part of PyCon before,
- 2:03attend it this year.
- 2:04Just visit talkpython.fm/PyCon 2021
- 2:08and enter your email address,
- 2:09and you'll be in the running for an individual PyCon ticket.
- 2:13compliments of Talk Python.
- 2:14These normally sell for about $100 each.
- 2:17And if you're certain you want to go,
- 2:18I encourage you to visit the PyCon website,
- 2:20get a ticket,
- 2:21and that money will go to support the PSF and the Python community.
- 2:24If you want to be in this drawing,
- 2:26just visit talkpython.fm/PyCon 2021.
- 2:29Enter your email address.
- 2:31You'll be in the running to win a ticket.
- 2:33Now let's get on to that interview.
- 2:35Min, welcome to Talk Python To Me.
- 2:38Thanks. Thanks for having me.
- 2:39Yeah, it's really good to have you here.
- 2:41I'm excited to talk about building applications with zero MQ.
- 2:45It's definitely one of those topics that I think lives in this realm of asynchronous programming
- 2:51in ways that I think a lot of people don't initially think of.
- 2:54Like you think of async programming,
- 2:56like, okay, well, I'm going to do threads.
- 2:57And if threads don't work,
- 2:58maybe I'll do multi-processing.
- 2:59And then those are my options, right?
- 3:01But Qs and other types of intermediaries are really interesting for creating powerful design patterns
- 3:07that let you scale your apps and do all sorts of interesting things, right?
- 3:10Yeah.
- 3:10Yeah.
- 3:11Zero MQ has definitely given me a new way of thinking about how different components
- 3:15of a distributed application can talk to each other.
- 3:17Absolutely.
- 3:18So we're going to dive all into that, which is going to be super fun.
- 3:21But before we do, maybe just tell us a bit about yourself.
- 3:24How do you get started in programming in Python?
- 3:26I got started mostly during college where I was studying physics
- 3:30and what essentially amounted to a computational physics degree.
- 3:33And that's where I met, I had a professor, Brian Granger,
- 3:37who's one of the heads of the now Jupyter project.
- 3:40I started working with him as an undergrad back in 2006,
- 3:43working on what would ultimately become the first IPython parallel,
- 3:47the interactive parallel computing toolkit in IPython.
- 3:50Oh, that's really cool.
- 3:50What university was that at?
- 3:52That was at Santa Clara University.
- 3:53Uh-huh.
- 3:53Okay.
- 3:54Nice.
- 3:54Yeah, it's really cool.
- 3:55I mean, you were working on physics,
- 3:56but you were also like at the heart of Jupyter.
- 3:59And I guess IPython at the time, right?
- 4:01IPython notebooks is what they were called, yeah?
- 4:03Yeah, coming up on 15 years that I've been working on IPython and Jupyter.
- 4:07Nice.
- 4:08And so you basically, did you do programming before you got into your physics
- 4:11or did you just learn that to get going with your degree?
- 4:14So I did it for fun in college,
- 4:17but I played around with calculators and stuff in high school,
- 4:20but nothing more complicated than printing out a Fibonacci sequence.
- 4:23Nice.
- 4:24So were you a TI person?
- 4:26Were you an HP person?
- 4:28What kind of world were you?
- 4:29Yeah, TI all the way.
- 4:31Yeah, me too, obviously.
- 4:32That reverse Polish notation, that was just wrong.
- 4:35Yeah.
- 4:35Although I did, in college, we did implement reverse Polish calculators
- 4:38because it's easier.
- 4:39Yeah.
- 4:39Yeah, cool.
- 4:40It's easy to write a parser for that.
- 4:42Yeah, it's easier on the creator and not on the user, which I feel like computers used to be way more that way, right?
- 4:48Like, oh, it's easier for us to make it this way.
- 4:49Well, if you put a little more work, it'll be easier for the millions of people that use it.
- 4:53Well, maybe not millions in the early, early days, but yeah.
- 4:55Very interesting.
- 4:57So what kind of stuff were you doing with IPython in the early days?
- 5:00What kind of stuff were you studying with your physics?
- 5:02In grad school, I was studying computational plasma physics simulation.
- 5:06So it was particle simulations of plasmas, which are, you know, what's going on in stars.
- 5:10I wasn't studying stars, but that's one of the main plasmas that people know about.
- 5:15And so I was doing particle simulations of just studying a system, seeing what happens.
- 5:19And we were working on an interactive scale simulation code that was written in C++.
- 5:24It was a fairly nice and welcoming C++ physics simulation as far as academic C++ physics simulations go.
- 5:32Yeah.
- 5:32But essentially, my first year of grad school, I wrote a paper where I had to do a simulation run for five days,
- 5:39look at the results, and then change some input parameters and run it again for five days,
- 5:43and do that for a few dozen iterations, and then wrote a paper about that.
- 5:47And then ultimately, my PhD thesis was about wrapping the same code in Python
- 5:51so that I could have programmatic, like, self-steering, actually by this time driven in a notebook.
- 5:56Doing what had been manual, like change it, run it again, doing that automatically,
- 6:02just by wrapping the C++ code in a Python API, I could do it much more efficiently and actually take something that took multiple iterations of five days,
- 6:11I could actually get the same result in a little over half a day, I think.
- 6:15Wow.
- 6:15That's really cool.
- 6:16So you would run basically a little small simulation with C, and then you would get the inputs into Python and go,
- 6:23okay, well, now how do we adjust it? How do we change that and go from there?
- 6:26It was the same simulation, but I had to tune the inputs a little bit differently in order to,
- 6:32there were some details of the physics that basically I had to turn some knobs way up because I wasn't constantly watching it.
- 6:38Yeah, I see.
- 6:39And then it would say like, oh, that number was too big, turn it down, do it again, and then look and see if it was too big again.
- 6:44Whereas once I had a knob that could be turned while the simulation was running, which is the feature that was missing,
- 6:51I could say ramp up the current in this case.
- 6:55So I was studying the limit of current that you can get through a diode.
- 6:59Right.
- 6:59And so ramp up the current, and then once I see evidence that it's too high, ramp it back down,
- 7:05and then I could change, you know, lower the slope so that I would slowly approach an equilibrium and find the limit.
- 7:11Whereas when I had to do it with this long, I had to basically wait until it's done to change the input,
- 7:17I had to dramatically over-inject the current, which made the simulations extra slow because there were way too many particles.
- 7:22Yeah, yeah.
- 7:23And the spatial resolution had to be really fine in order to resolve something called a virtual cathode,
- 7:28and I didn't have to do nearly as much.
- 7:30Basically, because I could do this live feedback, the simulations themselves could be quicker
- 7:36because I didn't have to provide enough information that I could get the right answer out a long time later.
- 7:42That's super cool, and obviously it makes a huge difference.
- 7:44You know, I feel like we have a similar background in how we got into programming.
- 7:48Like, I was doing a math degree, and my math research led me into doing C++ on Silicon Graphics, mainframes, computers, and stuff like that.
- 7:56You talk about letting these things run overnight.
- 7:58Like, everyone would just kick off these things, and then they would come back in the morning to see what happened.
- 8:04And, you know, interesting sort of random sidebars.
- 8:07Like, we came in one morning, and we all tried to log into our little workstations or whatever,
- 8:13and, like, the system just wouldn't respond.
- 8:15There was just something wrong with it.
- 8:16And we were like, well, what the heck is going on?
- 8:18You know, like, this is like a quarter-million-dollar computer.
- 8:20It should let us log in.
- 8:22What is wrong with it, right?
- 8:23And it was in there running really loud, right?
- 8:25It's like this huge, loud machine.
- 8:26And it turned out that one of the grad students, not me, had started a job that was—they were trying to figure out what was going on,
- 8:33and they were logging a lot because they were having problems with their code.
- 8:37And it was in this tight loop that ran all night.
- 8:40And because it was, like, a somewhat small group, they had no disk limits or permission restrictions for the large part.
- 8:47So it literally used up every byte on the server, and then it just wouldn't do anything at all.
- 8:52And, I mean, that's the—the reason I bring this up is, like, that's the challenge of these things where you, like,
- 8:57start it and let it run for days and then figure out what's happening.
- 9:00You're like, oh, it just used up the entire hard drive of this giant machine.
- 9:03Whoops.
- 9:03It broke it for everyone, right?
- 9:05Yeah.
- 9:05And so this live aspect or more self-guided is a really great idea.
- 9:10It's really—it's a big step ahead.
- 9:12Maybe more so than people initially, like, hear it as, right?
- 9:15It's a big deal.
- 9:16Yeah, and I think it was—that's part of what got me really excited about the programmatic—
- 9:21giving—when you have access to something in Python and you have an environment like iPython or Jupyter or whatever,
- 9:26being able to interact with something, what is usually—
- 9:30what you might think of as an offline physics simulation that runs for hours or days,
- 9:35if you have a Python API to it, you can steer it while it's going.
- 9:40Like, you can turn the knobs on your experiment while it's running.
- 9:44You know, sometimes there are reasons why that's not actually a good idea, but it's really powerful to not—
- 9:49one of the costs, you know, that I had in my experiment was that in order to change the inputs, I had to start over.
- 9:54Like, physically, I didn't have to.
- 9:56Like, if it were a physical machine where I were measuring inputs and outputs, I could just, you know, turn the current down and see the results.
- 10:02But the code didn't support that.
- 10:05The frustrating thing about it was the way the physics was written did support that.
- 10:08The C++ code did support that.
- 10:10We just hadn't written an interface that allowed me as the user to do it.
- 10:14And so the Python wrapping work was surprisingly little.
- 10:17It was, you know, expose Python APIs to—or C++ APIs to Python with Cython.
- 10:22Yeah, that's cool.
- 10:23And then I could turn all the knobs that were already there in the code.
- 10:25I just was now allowed to turn them.
- 10:27It sounds to me, thinking back on the timeline there, that must have been pretty early days in the data science, computational science world of Python, right?
- 10:36Like, NumPy was probably just about out around then.
- 10:39By the time I was doing that work, that would have been around 2012.
- 10:42Oh, okay.
- 10:43So, yeah, it was more established at that point.
- 10:45I was thinking back to, like, early, like, 2005 or something.
- 10:47That, yeah, not quite, right?
- 10:49Yeah, not quite.
- 10:49That was when I was still, as a little undergrad, was still able to help make building NumPy—I was able to submit patches to make building NumPy on Mac a little easier.
- 11:00That's cool.
- 11:00Before it became, like, completely polished and, like, so widely used.
- 11:04I suspect it's really nerve-wracking to contribute to those kinds of things now.
- 11:08It was a nice community, and it was exciting to be able to say, like, I have a problem.
- 11:12There's probably other people who have a similar problem.
- 11:14Let me see if I can fix it and then submit a patch.
- 11:16Yeah, that's fantastic.
- 11:17Cool.
- 11:17So you're carrying on with your scientific computation work these days, right?
- 11:22What do you go to now?
- 11:23Now my job is actually working on Jupyter and Python-related stuff.
- 11:27So I am a senior research engineer at Simula Research Lab in Oslo, Norway, where I've been since 2015.
- 11:34And I'm the head of the Department of Scientific Computing and Numerical Analysis.
- 11:38So I'm in a department where people are doing physical simulations of the brain and studying PEs and things like that.
- 11:45But what I do is I work on JupyterHub.
- 11:48Yeah.
- 11:48So we made JupyterHub as a tool for deploying while I was still at Berkeley.
- 11:53We made it as a tool for deploying—for people who want to deploy Jupyter Notebooks.
- 11:57We say it's for if you're a person who has computers and you have some humans and you want to help those humans use those computers with Jupyter.
- 12:04That's what JupyterHub is for.
- 12:05Initially, it was scoped to be an extremely small project because we didn't have any maintenance burden to spare.
- 12:11The target of that was research groups like mine in grad school of a few people, maybe small classes,
- 12:18who have one server in their office and they just want to make it easier for people to log in and use Jupyter on there.
- 12:23Right.
- 12:23This is when people—
- 12:24Right, right.
- 12:25People had all their SSH tunnel reverse—SSH to my server with the opener reverse tunnel so I can point localhost and it's actually over there.
- 12:32So we were trying to make life easier for those folks.
- 12:35They were running the servers locally and they're like, how do I get access to my computation over there but on my machine?
- 12:41That kind of stuff, right?
- 12:42Yeah, it's like if I'm a person who's already comfortable with Jupyter but now I have access to a computer that's over there, how do I run my notebook stuff over there?
- 12:50And there were a few solutions to that involving SSH tunnels or whatever.
- 12:54And JupyterHub was aimed at those folks.
- 12:56But it turned out that's not the user community that's really been the most excited about it.
- 13:02With things like Zero to JupyterHub, the JupyterHub on Kubernetes started by Yuvi Panda and now led by a whole bunch of wonderful folks, we're shifted into a larger scale than initially designed for.
- 13:13It definitely seems like JupyterLab and the predecessors to it have really probably exploded beyond the scale that people initially expected.
- 13:22I mean, it's just the de facto way of doing things if you're doing computation these days, it seems.
- 13:26I for certain folks definitely seem to like it.
- 13:29We try to respond to feedback and just build things that people are going to use.
- 13:33Nice.
- 13:33So if you're running JupyterLab for such a big group, I mean, there's got to be crazy amounts of computation and, you know, super computer type stuff there.
- 13:42What does your whole setup look like there?
- 13:43What are you running for your folks at the research lab?
- 13:46I run JupyterHub instances for like a summer school and they'll run a workshop and I'll spin up a cluster for running JupyterHub for doing some physics simulations.
- 13:55Usually it's for teaching.
- 13:56Okay.
- 13:57So I don't operate, I don't operate hubs for research.
- 13:59Usually I usually operate them for teaching.
- 14:01I see.
- 14:02Yeah.
- 14:02So maybe the computation there, it's like, yeah, not nearly as high as we're trying to model the universe and let that go.
- 14:08Yeah.
- 14:09Often those folks, the computations are handed off somewhere.
- 14:12Right.
- 14:13But yeah, like the folks at NERSC in Berkeley are doing some really exciting stuff with running JupyterHub on a supercomputer.
- 14:18Yeah.
- 14:19Cool.
- 14:19Are you doing like Kubernetes or Docker or are you just setting up VMs or how do you handle that side?
- 14:25Thanks to the work of all the folks contributing to Zero to JupyterHub, deploying JupyterHub with Kubernetes, I think, is the easiest to deploy and maintain as long as you have access to a managed Kubernetes.
- 14:37I still wouldn't recommend deploying Kubernetes itself to anybody.
- 14:40It seems fairly complicated to run and maintain that side of things.
- 14:43Yeah.
- 14:44But if you have a turnkey solution, the one I use the most is the, is GKE, the Google managed Kubernetes.
- 14:50Yeah.
- 14:50And Zero to JupyterHub will let you just step through and say, give me a cluster and run JupyterHub on it.
- 14:56And then all I really need to do is for the workshop is build the user images.
- 14:59Nice.
- 15:00Very cool.
- 15:00You also do some stuff with Binder.
- 15:02Is that right?
- 15:02I help operate Binder.
- 15:04So mybinder.org is a service built on top of JupyterHub that takes JupyterHub as a service for running notebooks on Kubernetes.
- 15:12In Binder's case, it's running on Kubernetes.
- 15:14And Binder ties in another Jupyter project called repo2docker that says, look at a repo, build a Docker image with the contents of that.
- 15:21Hopefully that can run everything.
- 15:23So like if it finds a requirement set txt.
- 15:25Yeah, exactly.
- 15:26It's got something that specifies its dependencies or something like that.
- 15:28Yeah.
- 15:28And there's a bunch of things that we support.
- 15:30And the idea is to automate existing best practices, right?
- 15:33So find anything that people are already using to specify environments and then install those and then build an image.
- 15:39And then what Binder does is, so I built an image, then send that over to JupyterHub to say launch a notebook server with that image.
- 15:45Right.
- 15:46Yeah.
- 15:46You can see like right here just on the main site, just put in a GitHub repo and maybe a branch or a tag or something and then click go.
- 15:53And then it spins up literally a Jupyter notebook that you can play with.
- 15:57This portion of Talk Python To Me is sponsored by Linode.
- 16:01Simplify your infrastructure and cut your cloud bills in half with Linode's Linux virtual machines.
- 16:05Develop, deploy, and scale your modern applications faster and easier.
- 16:09Whether you're developing a personal project or managing large workloads, you deserve simple, affordable, and accessible cloud computing solutions.
- 16:17As listeners of Talk Python To Me, you'll get a $100 free credit.
- 16:21You can find all the details at talkpython.fm/Linode.
- 16:25Linode has data centers around the world with the same simple and consistent pricing regardless of location.
- 16:31Just choose the data center that's nearest to your users.
- 16:34You'll also receive 24-7, 365 human support with no tiers or handoffs regardless of your plan size.
- 16:40You can choose shared and dedicated compute instances or you can use your $100 in credit on S3 compatible object storage, managed Kubernetes clusters, and more.
- 16:51If it runs on Linux, it runs on Linode.
- 16:53Visit talkpython.fm/Linode or click the link in your show notes.
- 16:58Then click that create free account button to get started.
- 17:03When people go to GitHub, they can oftentimes see the Jupyter Notebook.
- 17:07When I first saw that, I'm like, how in the world is GitHub computing this stuff for me to see?
- 17:13I'm like, maybe that is super computationally expensive.
- 17:17Here's the answer.
- 17:18How do they know the data and the dependencies?
- 17:21The reality is they've just taken what's stored in the Notebook, right?
- 17:25That is the last run and it's there.
- 17:28But if you want to play with it, you can't do that on GitHub.
- 17:29But you can on mybinder.org, right?
- 17:32That turns that into an interactive Notebook.
- 17:35It adds interactivity to the sharing that you already have with NVMe or GitHub.
- 17:39Yeah, very cool project.
- 17:40I didn't know that much about it, but I had Tim Head on the show a while ago on episode 256 to talk about that.
- 17:47And I learned a lot.
- 17:47So, yeah, quite neat.
- 17:49Now, let's jump into 0MQ.
- 17:51So 0MQ is not a Python thing, but it is very good for Python people, right?
- 17:58It has support for many different languages.
- 18:00And your work primarily has been to work on making this nice and easy from Python, right?
- 18:06Yeah, so 0MQ is a C++, a library written in C++ with a C API, which makes using it a little easier.
- 18:12And it's a very small API, which is part of why it's usable from so many languages,
- 18:17is that writing bindings for it is relatively easy.
- 18:20The idea of 0MQ is it's a messaging library.
- 18:25Naming is a little funky because it comes from the world, right?
- 18:30The people who create it come from the world of message brokers and message queues.
- 18:35And 0MQ is a bit tongue-in-cheek in that it's not actually a message queue at all.
- 18:40It's a messaging library where it's just adding a little bit of a layer of abstraction on the networking in terms of you have some distributed application where things need to talk to each other.
- 18:50And 0MQ is a tool for building that.
- 18:52And it's this library.
- 18:54And then you can write the bindings for that library so that you can use it from any of a variety of languages.
- 19:00And so Brian Granger and I worked on the Python bindings to use 0MQ from Python, which is called PyZMQ.
- 19:07Yeah, when I first thought of it, I imagined it is like a server.
- 19:10Something like Redis or Celery or something like that that you start up.
- 19:15And then you create queues or something on it.
- 19:17And then like different things can talk to it.
- 19:19But I think maybe a better conceptualization of it might be like Flask.
- 19:23Flask is not a server, but a Flask is a framework that you can put into your Python app and then run it.
- 19:30And it is the server itself, right?
- 19:32Yeah.
- 19:32And so I'd say that 0MQ, the best way I would describe it is it's a fancy socket library.
- 19:37Yeah.
- 19:37So you create sockets and you send messages.
- 19:39And the sockets talk to other sockets.
- 19:41You send messages, you receive messages.
- 19:44And then 0MQ is all about what abstractions and guarantees and things it gives around those sockets and messages.
- 19:51There seems to be a lot of culture and zen about 0MQ.
- 19:55Like there's a lot of interesting like nomenclature in the way that they talk about stuff over there.
- 20:00So they talk about the zero in 0MQ.
- 20:03And the philosophy starts with zero.
- 20:05Zero is for the zero broker.
- 20:06It's brokerless.
- 20:07Zero latency, zero cost.
- 20:09It's free.
- 20:10Zero admin.
- 20:11You don't have like a server type of thing.
- 20:13But also to a culture of minimalism that permeates the project.
- 20:16Adding power by removing complexity rather than exposing new functionality.
- 20:20You want to speak to that just a little bit?
- 20:22Like your experience with that?
- 20:23Well, so I can speak to that.
- 20:25As someone who doesn't work on LibZMQ that much and more somebody who writes bindings for it.
- 20:31I can speak to that as it's nice that LibZMQ doesn't change that much.
- 20:35Yeah.
- 20:35And that part of point of, so, ZMQ has all these features.
- 20:39They have, we'll talk a little bit about it in a second.
- 20:42But so it's structured so that there are sockets and there are different kinds of sockets that have different behaviors for building these different kinds of distributed applications.
- 20:51But in terms of the API, there's just one.
- 20:53Like a socket has an API.
- 20:55All sockets have the same API.
- 20:57So from the standpoint of writing bindings to a library, I just need to say, I know how to wrap the socket APIs.
- 21:05And then as they add new types, those are just constants that I need to handle.
- 21:09So I don't need to, oh, there's a new kind of socket.
- 21:11I need to implement a new Python class for that new kind of socket.
- 21:14I just need to wrap socket.
- 21:16So that's really, that's from my perspective in terms of as new features are developed and things in LibZMQ, from my perspective as a binding developer or binding maintainer, that's really nice.
- 21:28But it also, that also extends to the application layer.
- 21:31That once you have a socket and you understand sockets, changing the type of socket changes your message pattern.
- 21:36It doesn't change anything about the APIs you need to use and things like that.
- 21:40Yeah, so maybe we could talk a little bit about what the application model, I don't necessarily get into programming yet, but like the application model, right?
- 21:48So we've got contexts, we've got sockets, and we've got messages.
- 21:52And those are the basic building blocks of what working with this is like.
- 21:55So if I wanted to create something that could, you know, maybe other applications could talk to it and exchange data, one of my options might be to create a RESTful API that exchanges JSON, right?
- 22:06Yeah, sure.
- 22:07There's a lot of challenges with that.
- 22:09One, it's sort of send request only, right?
- 22:12I send over my JSON and then it gives me a response, but I can't subscribe to future changes, right?
- 22:19I got to do something like WebSockets in that world if I want something like that, which I guess might be closer to this.
- 22:24And then also it's doing the text conversion.
- 22:27It's probably a little bit slower.
- 22:28Got to do maybe extra work for async, right?
- 22:31So there's a lot of things that are maybe similar, but extra patterns, right?
- 22:36So instead of just request response, you might have pub sub, you might have like multicast, like something comes in and everyone gets notified about it.
- 22:45Can you talk about some of those differences?
- 22:47Like maybe compare it to what other more common APIs people might know about?
- 22:51Yeah.
- 22:52So the main thing that distinguishes ZeroMQ is that you have you, so a context is kind of an implementation detail that you shouldn't need to care about, but you still need to create.
- 23:00Okay.
- 23:00Sockets are the main thing that you deal with and you create a, so you create a socket and every socket has a type and that type determines the messaging pattern.
- 23:10So that means that's where we're getting at these kinds of protocols and messaging patterns.
- 23:15So with a web server, that's usually a request reply pattern.
- 23:19So you have clients connect, send a request, and then they get a reply.
- 23:23Pub sub system might be, you know, some totally different thing.
- 23:26Maybe in web server land, maybe it's a server side events, you know, an event stream connection.
- 23:32And with ZeroMQ, the difference between those is the socket type.
- 23:36So if you're creating a publish subscribe relationship, you create a publish socket on one side and you create a subscribe socket on the other side.
- 23:44If you're doing a request reply, you use a socket called a dealer and a router.
- 23:49There's a request and a reply socket in ZeroMQ, but nobody should ever use them.
- 23:53They're just a special case of router dealer.
- 23:55And then there's another one called a pattern called like a ventilator and sink.
- 23:58So that's like what you'd use in a work queue, for instance, where you've got a source of work and then it sends a message to one destination, but you don't necessarily care which one.
- 24:09I see.
- 24:10So maybe you're trying to do a scaled out computing.
- 24:13Yeah, exactly.
- 24:13You've got 10 machines that could all do the work and you want to somehow evenly distribute that work, right?
- 24:20So you're like, all right, well, we're just going to throw it at ZeroMQ.
- 24:23All the things that are available to do work can subscribe.
- 24:26They need to.
- 24:27They could even like drop out after doing some work, but then not receive anymore, potentially something like that.
- 24:33So that's what ZeroMQ, one of the main things that ZeroMQ does is it takes control over those things like multiple peers and connection events and stuff like that.
- 24:43Because take, for example, the publish subscribe.
- 24:46There's two key.
- 24:48The things to think about with ZeroMQ are what happens when you've got more than one peer connected.
- 24:54Right.
- 24:55And what happens when you've got nobody to send to.
- 24:57So in the publish subscribe model, what it does is when you send a message on a socket, it will send it will immediately send that message to everybody who's connected and ready.
- 25:06So if somebody is not able to keep up, right, there's like a queue that's building up and it's gotten full, it'll just drop, stop, drop messages to that peer until they catch up.
- 25:15If there are no peers, then it's really fast because it just doesn't send anything and just deletes the memory.
- 25:20Yeah.
- 25:20Right.
- 25:21So with when you're thinking of ZeroMQ from Python, sending messages is really the other thing about it is that it's asynchronous.
- 25:29That send is not actually send doesn't return when the message is on the TCP buffer or whatever.
- 25:36Send returns when you have handed control of the message to the ZeroMQ IO thread.
- 25:42And this is why ZeroMQ has this concept of context.
- 25:45Contexts are what own the IO threads that actually do all the real work of talking to over the network.
- 25:51So when you're sending in PyZMQ, you're really just passing ownership of the memory to ZeroMQ and it returns immediately.
- 25:58And so you don't actually know when that message is actually is finally sent and you shouldn't care.
- 26:03Sometimes you do care and it can get complicated.
- 26:05Yeah.
- 26:05But ZeroMQ tries to make you not care.
- 26:07Interesting.
- 26:08So basically you set up the relationships between the clients and the server through like these different models.
- 26:14And then you drop off the messages to ZeroMQ and it just it deals from there.
- 26:19Right.
- 26:19It figures out who gets what and when.
- 26:22So your application, as soon as it gets the message passed off to the ZeroMQ layer, it can go about doing other stuff, right?
- 26:29Exactly.
- 26:30So ZeroMQ, because it's a C++ library, it's not going to grab the GIL or anything.
- 26:35So it's a true, even if you're using Python, it's a true multi-threaded application, even if you're only using one Python thread.
- 26:40Yeah.
- 26:41You handed that memory off to C++, which is running in the background.
- 26:43You can do some GIL holding intense operation and ZeroMQ will be happily dealing with all the network stuff.
- 26:49Right.
- 26:50Right.
- 26:50It's got its own C++ thread, which has nothing to do with the GIL and they go do its own thing, right?
- 26:55Yeah, it can, there's, there is something that comes up in PyZMQ where it can come back and try to grab the GIL from the IO thread or it used to, it doesn't anymore because of Python does actually need to know when it let go of that memory in order to avoid segfaults.
- 27:09Yeah.
- 27:09But that's an implementation detail.
- 27:10Cool.
- 27:11So is there anything about reliable messaging here where you could say, I want to make sure that this message gets delivered to every client?
- 27:20Like if you said, for example, in this PubSub, if some of them fall behind, it can just drop the messages.
- 27:24Is there a way to say, you know, pile that up and then send it along when it catches up or whatever?
- 27:30The ZeroMQ perspective is that that's an application level problem.
- 27:33So ZeroMQ helps you build the messaging layer to not, so that it doesn't crash.
- 27:39And that's part of why it drops messages.
- 27:41And then it basically, it's up to you to say, you know, if you're sending messages and there's a generation counter, for instance, and then you notice I got message five and then I got message eight.
- 27:51It's up to your application to say, okay, keep a, you know, keep a recent history buffer so that folks can come back with a different pattern, a request reply pattern to say, give me a batch of recent messages that I missed so that I can resume.
- 28:06But ZeroMQ doesn't help you with that.
- 28:08It handles all the networking stuff, you know, connections.
- 28:11You don't have to worry about that.
- 28:12You just have to worry about like, how do we set up a way to ask again or have the application ask if it wants more.
- 28:17Yeah.
- 28:17Nice.
- 28:18David out there on the live stream asks, what alternatives to ZeroMQ could have been used to build the Jupyter protocol?
- 28:24So maybe before we, thanks for the question.
- 28:27Before we get to that though, maybe let's just talk about like, oh wait, ZeroMQ was used to build the Jupyter protocol?
- 28:33Yeah.
- 28:33So the Jupyter protocol, which essentially started out in IPython parallel.
- 28:37So we had this interactive parallel computing networking framework that eventually evolved into, wait, we've got this network protocol for remote computation.
- 28:46We can build basically a REPL protocol, an interactive shell protocol.
- 28:51And that ultimately became the Jupyter protocol, which was built with this kind of ZeroMQ mindset of, I want to be able to have multiple front ends at the same time.
- 29:03So let's say, and we had in 2010, I think, Fernando and Brian had a working prototype of real-time collaboration on a terminal.
- 29:11So you've got, using this protocol, so you've got two people with the terminal, you're typing, you can run code and you can see each other's output.
- 29:19So we built the Jupyter protocol with multiple connections in mind.
- 29:24So that means there's this request reply socket.
- 29:26So the front end sends a request, please run this code.
- 29:30And the back end sends a reply saying, I ran that, here's the result.
- 29:34And there's another channel called IOPub where we publish output.
- 29:38So when you do a print statement or you display a matplotlib figure, that's a message that goes on PubSub channel, which means that every connected front end, right?
- 29:49So you could have multiple JupyterLab instances.
- 29:51They'll all receive the same message.
- 29:53Right.
- 29:53I mean, that sounds like the perfect example of PubSub.
- 29:56Somebody's making a change.
- 29:57Well, somebody has triggered the server to make a change, but it doesn't matter who started that change.
- 30:03Everybody looking at it wants to see the output, right?
- 30:06This portion of Talk Python To Me is brought to you by Mido.
- 30:09You feel like you're stumbling around trying to work with pandas within your Jupyter notebooks?
- 30:13What if you could work with your data frames visually like they were Excel spreadsheets, but have it write the Python code for you?
- 30:19With Mido, you can.
- 30:21Mido is a visual front end inside Jupyter notebooks that automatically generates the equivalent Python code within your notebook cells.
- 30:28Mido lets you generate production ready Python just by editing a spreadsheet, all right with inside Jupyter.
- 30:35You're sure to learn some interesting Python pandas tricks just by using the visual aspects of that spreadsheet.
- 30:42You can merge, pivot, filter, sort, clean, and create graphs all in the front end and get the equivalent Python code written right in your notebook.
- 30:49So stop spending your time Googling all that syntax and try Mido today.
- 30:53Just visit talkpython.fm/Mido to get early access.
- 30:57That's talkpython.fm/M-I-T-O or just click their link in the show notes.
- 31:04So to answer the question of, and this becomes particularly important in the design of IPython parallel, but getting to the question of what alternatives to 0.mq could have been used for the Jupyter protocol.
- 31:15So when we were designing it, we thought this protocol of talking directly to kernels was going to be the main thing.
- 31:20It turns out that the main way, you know, in 2020 or 2021, I guess we're in now.
- 31:25The most kernels talk one-to-one with a notebook web server.
- 31:30And it's the web server that's the one that's actually spanning out to multiple clients.
- 31:34Right.
- 31:35With that being the case, if we had required the notebook server to be the place where we do all this multiplexing and everything, we could have actually built the lower level Jupyter protocol on something much simpler, just an HTTP rest and event stream.
- 31:49Probably could have worked just fine.
- 31:50Right.
- 31:51Maybe just use WebSockets or something on the web server side.
- 31:54Yeah.
- 31:54If WebSockets had existed at the time.
- 31:56Yeah, yeah, yeah.
- 31:57Those were also years away.
- 31:59Yeah.
- 32:00So much of that stuff's easier now and the browser support it and so on.
- 32:03So yeah, super interesting.
- 32:04Now, one thing that that makes me think of is, you know, how much, how close are we or some sort of Google Docs, JupyterLab type of thing, right?
- 32:15I mean, we're not there, right?
- 32:16I know there's SageMath and there's Google Colab.
- 32:19There's other systems where this does exist, right?
- 32:23Where there's sort of, we can all type on the notebook, same cell, same time type of thing.
- 32:27Is there anything like that, JupyterLab, that I just didn't miss or I missed?
- 32:31There have been, I think, three prototypes at this point that have been developed and are, and ultimately not finished for various reasons.
- 32:39There's another one that's picking up again and going strong using Yjs, working with QuantStack, I believe.
- 32:45Okay.
- 32:45Hopefully soon.
- 32:46That's not an area of the project where I've done a lot of work.
- 32:48I helped a little bit with the last one.
- 32:50Well, I mean, to me, it sounds like that's like just all JavaScript front end craziness and not a whole lot of other stuff, right?
- 32:56Yeah, the state probably lives on the server, so you need to have a server, whether it's running CRDT or whatever, to synchronize the state.
- 33:03And the PubSub, yeah, as well for the changes.
- 33:05Yeah.
- 33:06Going down this rabbit hole for a minute, also, Nawa asked an interesting question about 0MQ.
- 33:12What's the story with 0MQ and microservices, right?
- 33:16And I think microservices, people often set up a whole bunch of little small flask or FastAPI things that talk JSON exchange request response.
- 33:25But man, like the performance and the multiplexing, all those types of things sound like it actually could be a really awesome non-HTTP-based microservice.
- 33:35Yeah, I think 0MQ is a really good fit for microservice-based distributed applications.
- 33:40Because one of the things you do when you're designing with microservices is you're defining the communication relationship and you're scaling axes.
- 33:48And a nice thing to do with 0MQ is to say is that your application doesn't change when you've got a bunch of peers.
- 33:57Your application doesn't even need to know when a new peer comes and goes because 0MQ handles that.
- 34:01So one of the things that's nifty about, that's weird and magical, but also really useful about 0MQ is it abstracts binding and connecting and transports.
- 34:11So you can have the same application with the same connection pattern and maybe this one binds and maybe, you know, one side binds and one side connects.
- 34:19So pub binds and sub connects.
- 34:21But you can also have sub bind and pub connect.
- 34:24And you can also have your pub bind once and connect three times.
- 34:27And none of that changes how your application behaves.
- 34:30It just changes where the connections go.
- 34:32Right.
- 34:32Because with microservices, when you're doing HTTP requests, you always got to figure out, okay, well, what's the URL I'm going to?
- 34:38And sometimes that even gets real tricky with, all right, what is even the URL of the identity server?
- 34:43What is the URL of the thing that manages the catalog before I even request it?
- 34:48And then usually that's a single endpoint HTTP request type of thing if it's like an update notification.
- 34:53So yeah, I can imagine that there's some real interesting things here.
- 34:56You can have in, you know, a distributed work kind of situation, you can have one or a few sources of work.
- 35:03And then you have an elastic number of workers that just connect and start receiving messages.
- 35:08And the way this is in kind of the push-pull pattern.
- 35:11So pub sub is always send them every message you send, send it to everybody connected who can receive a message.
- 35:18Whereas push-pull is whenever you send a message, send it to exactly one peer.
- 35:22And I don't care which one.
- 35:23And so if more peers are connected, it will load balance across all those peers.
- 35:27But if only one's connected, it will just keep sending to that one.
- 35:29And at no point in the sender do you ever need to know how many.
- 35:35You never get notified that peers are connecting.
- 35:38You never need to know that there are any peers, that there's one peer, that there's a thousand peers.
- 35:42It doesn't matter.
- 35:44So then it's in your distributed application to say, you know, this one's sending with this push-pull pattern, ventilator sync pattern.
- 35:51And then I just elastically grow my number of workers and shut them down.
- 35:55And they just connect and close, disconnect and everything.
- 35:58And it just works.
- 35:59Interesting.
- 36:00Okay, so a follow-up question from Nawa that makes me think.
- 36:04So he asks, he, she, sorry, asks whether it is a good idea to replace REST communication with ZeroMQ.
- 36:11And so that leads me to wonder.
- 36:13You talked about, you send the message and it's sort of fire and forget style, like message sent success.
- 36:18But so often what I want to do is I need to know what products are offered on sale right now from the sale microservice or whatever.
- 36:26I need to get the answer back.
- 36:28These three products.
- 36:29Thank you.
- 36:30You know what I mean?
- 36:31How do I implement something like that where I send a message, but I want the answer?
- 36:35That's the request reply pattern.
- 36:37So you would use either the request reply sockets or the router dealer sockets for that kind of pattern.
- 36:44And that's, you send a message.
- 36:45So in that case, this does have multi-peer semantics, but usually the requester is connected to one endpoint.
- 36:52It can be connected to several, in which case it'll load balance its requests.
- 36:57And the router, so the receiver side, handles requests.
- 37:00And each request comes in with the message prefix that identifies who that message came from.
- 37:07And then it can send replies using that identity prefix.
- 37:10And it will go to whoever sent that request.
- 37:13And that's how most of the Jupyter protocol is a request reply pattern.
- 37:16Yeah, I guess so.
- 37:17That makes sense because you want the answer from the computation or whatever.
- 37:20You want to know that it's done and so on.
- 37:22Interesting.
- 37:22Okay.
- 37:23Yeah.
- 37:23Well, that's pretty neat.
- 37:25The other thing that comes to mind around this is serialization.
- 37:28So when I'm doing microservices, I make JSON documents.
- 37:32I know what things go in JSON, right?
- 37:34Like fundamental types, strings, integers, and so on.
- 37:37Surprisingly, dates and times can't go into JSON.
- 37:41That blow.
- 37:41I still like, this is 2021.
- 37:43We can't come up with a text representation of what time it is.
- 37:46Anyway, that's a bit of a pain.
- 37:48It seems to me like you might be able to exchange more data more efficiently using a binary.
- 37:54And it's not even going over the HTTP layer, right?
- 37:57It's going literally over a TCP socket.
- 38:00Yeah.
- 38:00Or IPC with BSD sockets or UDP.
- 38:03Even lower level than that.
- 38:04Yeah.
- 38:04Not even touching the network stack.
- 38:06Right.
- 38:06Yeah.
- 38:06This gets us to the last piece of ZeroMQ that we haven't talked about yet.
- 38:10And that's what is a message.
- 38:11Right.
- 38:12They're nice things.
- 38:13You know, if you've worked with, you know, a lower level socket library, just talking TCP
- 38:17sockets, you know, you have to deal with like chunks and then figure out when you're done
- 38:21with your message protocol.
- 38:22But if you've ever written an HTTP server, you know, you need to find those double blank lines
- 38:27and all that stuff before you know that you have a request that you can hand off to your
- 38:31request handler.
- 38:32Right.
- 38:32Right.
- 38:32You're doing a whole lot of funky, like parsing the header.
- 38:35Okay.
- 38:36The header, it says it's the next hundred bytes of the thing that I'm getting.
- 38:39And this one is an end.
- 38:40So I'm at a part like it's, it's gnarly stuff.
- 38:42I've worked on projects where we did that and it's super fast, but boy, it is a, it's a low
- 38:47level business.
- 38:47Something like Flask does for you.
- 38:49Yeah.
- 38:49Right.
- 38:50It, it implements the HTTP protocol and then says, okay, here's a request.
- 38:53Please send a reply.
- 38:54And it helps you construct that reply message.
- 38:57So zero MQ or PyZMQ live at that, at that level of Flask where a message is a, not one
- 39:05binary blob, but a collection of binary blobs.
- 39:09And zero MQ always delivers whole messages.
- 39:13So it's, it's atomic and it's asynchronous and it's messaging, which means you will never
- 39:17get part of a message.
- 39:18There's no like, okay, I got the first third of this message, keep it in my own buffer until
- 39:24I get the, you know, get to the end.
- 39:26A zero MQ socket does not become readable until an entire message is ready to be read.
- 39:31Right.
- 39:31And there may be buffering down the C++ layer, but it's not going to tell you I've received
- 39:35a thing until it's fully baked.
- 39:37Got the answer.
- 39:38Right.
- 39:38Right.
- 39:39Yeah.
- 39:39All that stuff still happens.
- 39:40It's just zero MQ takes care of that.
- 39:42And then when, when at the PyZMQ level or at the ZMQ API level, when a socket, when you receive
- 39:49with a socket or in PyZMQ, you say receive multi-part, you get a list of blobs of memory.
- 39:55And so if you're talking about serialization with Jason, so PyZMQ has a helper function
- 40:00called send Jason, and it's literally just Jason that dumps thing and then send it.
- 40:05Yeah.
- 40:05With a little ensuring UTF eight bytes, I think.
- 40:07Yeah.
- 40:07Yeah.
- 40:08Nice.
- 40:08So PyZMQ, it's, and this is really turned out to be really important for more important
- 40:15for IPython parallel than it turned out to be for the Jupyter protocol.
- 40:19I'm not familiar with IPython parallel.
- 40:21Tell me about this.
- 40:22IPython parallel is, so if you're aware of the Jupyter protocol, it's a network protocol
- 40:27for, I've got somewhere over the network where I want to run code.
- 40:30And I have this protocol for sending messages.
- 40:33Please run this code.
- 40:34Give me a return output.
- 40:35Show me display stuff.
- 40:37IPython parallel is a kind of weird parallel computing library based on the fact that,
- 40:41so I've got a network protocol to talk to, to run code remotely.
- 40:45Why don't I just wrap that in a little bit to talk to N remote places.
- 40:49Maybe partition up the work across them or something like that.
- 40:52The fun thing about ZeroMQ is that in a Jupyter notebook, the kernel is the server.
- 40:57The kernel listens for connections on its various sockets.
- 41:00And then the notebook server, the web server, or the Qt console, or the terminal is a client,
- 41:06and it connects to those sockets.
- 41:08So I Python parallel, because of this fun stuff about ZeroMQ not caring about connection,
- 41:12direction, or count, adds a scheduler layer, and it modifies the kernel, the IPython kernel
- 41:19that you'd use in a Jupyter notebook.
- 41:20And the only change it makes is instead of binding on those sockets, it connects to a central scheduler.
- 41:27And the kernel is otherwise identical.
- 41:28The message protocol is otherwise identical.
- 41:30But the connection direction is different because the many-to-one relationship is different.
- 41:35There's one controller and many engines instead of many clients connecting to one kernel.
- 41:40And then, again, using kind of some of the magic of the ZeroMQ routing identities,
- 41:45there's a multiplexer in PyZMQ called a monitored queue, where if you have router socket,
- 41:54so a router socket is one where the first...
- 41:56So we talked about a ZeroMQ message is a sequence of blobs of memory.
- 42:00So it can just be one.
- 42:01With a router socket, it's always at least two,
- 42:03because the first part is the routing identity to tell the underlying ZeroMQ
- 42:07which peer should it actually send to.
- 42:10Sure, okay.
- 42:10That's cool.
- 42:11And we don't have to worry about that, because that's down at the low level, right?
- 42:14But that's what happens.
- 42:15When you get a request, you need to remember that first part.
- 42:17So when you send the reply, the first part of the reply is the ID that came at the request.
- 42:22Got it, got it.
- 42:22So it goes back to the right place.
- 42:23Yeah.
- 42:23But you can also use that if you know the IDs,
- 42:26you can send messages to a destination without being in response to a request, right?
- 42:32What a router really is, is a socket that can route messages based on this identity prefix.
- 42:37So if you have a bundle of identity prefixes, then you can send messages to anyone at any time.
- 42:41And that allows us to build a multiplexing scheduler that from one client connected to one scheduler,
- 42:48just send messages, regular, plain old ZeroMQ protocol messages,
- 42:52but with an identity prefix from the client.
- 42:55And those will end up at the right kernel just by the magic of ZeroMQ routing identities.
- 42:59Yeah, yeah.
- 43:00And so this is a substantially different messaging pattern.
- 43:04So the request reply patterns are all the same, but the connection patterns are totally different.
- 43:09And the client and the endpoint don't need to know about it at all.
- 43:13We just have this adapter in the middle.
- 43:15I feel like to really get the zen of this and take full advantage,
- 43:18you've got to really think about these messaging patterns and styles a little bit,
- 43:22because they're fairly different than, oh, this is what I know from web servers.
- 43:25So the big thing to do with, if you're getting into ZeroMQ is to read the, there's something called the guide.
- 43:29And there'll be a link in the notes.
- 43:32And if you go to zerumq.org, it'll be prominently linked.
- 43:34And this goes through kind of the different patterns that ZeroMQ thinks about,
- 43:38the abstractions in ZeroMQ, and the different socket types and what they're for.
- 43:43And the guide will help you.
- 43:44And there are examples in many languages, including Python.
- 43:47Yeah, this seems great.
- 43:48Yeah.
- 43:48It'll help you build kind of little toy example patterns of here's a publish subscribe application.
- 43:54Here's a ventilator sync application.
- 43:57And then it also does things with pictures.
- 44:00Yes.
- 44:01That's, I think, the way to internalize what are the ZeroMQ concepts and how do I deal with this.
- 44:06So when it comes to serialization, this is really important for iPython Parallel.
- 44:10And it also comes up if you're in Jupyter and use the interactive widgets,
- 44:13if you use the really intense ones that do like 3D visualization,
- 44:17interactive 3D visualization in the browser that sometimes are streaming a lot of data from the kernel.
- 44:22Because a ZeroMQ, this combines two things, one from PyZMQ and one from ZeroMQ itself.
- 44:27So the ZeroMQ concept that a message is actually a collection of frames.
- 44:31This lets you and another that ZeroMQ can be zero copy and PyZMQ supports zero copy.
- 44:38So anything that supports the Python buffer interface can be sent without copying,
- 44:42meaning it's still copied over the network, but at no point are there any copies in memory.
- 44:47So you can send 100 megabyte NumPy array with ZeroMQ without copying it.
- 44:51But then you've got to think about, oh, wait, if I send a NumPy array using the Python buffer interface,
- 44:57all I got were the bytes.
- 44:58Where are, you know, where's the D type information?
- 45:01Like, how do I know this is a 2D array of integers?
- 45:04Because a message is in Python language is a list of chunk of blobs instead of a single blob.
- 45:11You can serialize that metadata as like a header and the blob you don't want to copy, the big one, separately.
- 45:17So you can say like, Jason dumps the, some message metadata that tells you how to interpret the binary blob and then just the binary blob and you don't copy it.
- 45:27So then you can send as one message, right?
- 45:30We're not breaking the single message delivery.
- 45:32You have your metadata that's serialized with message pack.
- 45:36That comes in as like a frame or something like that in the message.
- 45:38Yeah.
- 45:39Yeah.
- 45:39So one frame is your header.
- 45:41One frame is the data itself.
- 45:43And we do this in the Jupyter protocol that the Jupyter protocol has an arbitrary number of buffers on the end.
- 45:48But then there are three frames that are actually Jason serialized dictionaries.
- 45:53Very cool.
- 45:53Very cool.
- 45:53Yeah.
- 45:54Looking at the guide here, it says there's 60 diagrams with 750 examples in 28 languages.
- 46:01That's a big cross product matrix of options in here.
- 46:04And you can also download it as a PDF to take with you, which, yeah, this looks like a really great place to get started.
- 46:10Speaking of getting started, let's talk about programming with the Python aspect here.
- 46:15All right.
- 46:16So here we'll use PyZMQ and this is a library that you work on as well.
- 46:21Yeah.
- 46:21Yeah.
- 46:21I maintain PyZMQ.
- 46:23Yeah.
- 46:23Awesome.
- 46:23So maybe, you know, it's hard to talk about code, but just give us a sense of what it's like to create a server.
- 46:30Like in Flask, you know, I say app equals Flask.
- 46:33Then I decorate app.route on a function.
- 46:36Like what's the 0MQ Python equivalent of that?
- 46:40Yeah.
- 46:40So first you always have to create a context and then use that context as a socket method that creates sockets.
- 46:46And then you either bind or connect those sockets.
- 46:50And then you start sending and receiving messages.
- 46:52So if you're writing a server, which usually means this is the one that binds.
- 46:57Yeah.
- 46:58So you'd create a socket.
- 46:59You'd call socket.bind and give it a URL, maybe a TCP URL or an IPC URL with a path, you know, a local path.
- 47:05And then you'd go into a loop saying, you know, receive a message, handle that message, send a reply.
- 47:11Or if it's a publisher.
- 47:13They often have a while true loop sort of thing, right?
- 47:16Just while true, wait for somebody to talk to me or while not exit.
- 47:19Yeah.
- 47:20That's a simple version.
- 47:20Or you could be integrated into asyncio or tornado or G event or whatever.
- 47:26Yeah.
- 47:27Yeah.
- 47:27One of the fundamental principles of 0MQ is that it's async all over the place.
- 47:31What's the async and await story with PyZMQ?
- 47:35Is there any integration there?
- 47:36Yeah.
- 47:37So if you do import zmq.asyncio instead of, if you do import zmq.asyncio as ZMQ, you will have the same thing, but send and receive are awaitable instead.
- 47:47Oh, that's glorious.
- 47:48Yeah.
- 47:49That's really, really fantastic.
- 47:50So you should be able to scale that to handling lots of concurrent exchanges.
- 47:54Pretty straightforward, right?
- 47:56Yeah.
- 47:56And that's how the, so taking Jupyter as an example again.
- 47:59So the Jupyter notebook uses the tornado framework, which if you're getting into asyncio, tornado is basically asyncio.
- 48:06Right.
- 48:07Yeah.
- 48:07It's the early days, early take asyncio.
- 48:10Yeah.
- 48:10And there we use something called a ZMQ stream, which is a, something inspired by tornadoes IO stream, which is their wrapper around a regular socket.
- 48:19That's like bytes are coming in, call events when bytes have arrived.
- 48:23ZMQ stream is a tornado thing that says when you have an on receive method that passes a callback, it says whenever there's a message, call this callback with the, with the message after receiving it.
- 48:35Yeah.
- 48:35And so that's actually how the IPython kernel and Jupyter notebook work on the ZMQ side is with these ZMQ stream objects.
- 48:41Cool.
- 48:42So the example you talked about is how to create a server, but you know, web version would be use request to do a request.get against the server to be the client that talks to it.
- 48:53What's the, that version in PyZMQ?
- 48:55In PyZMQ, a client looks very much like a server, except instead of bind, you'd call connect.
- 49:01And instead of receive, you do a send.
- 49:02Yeah.
- 49:03Yeah.
- 49:03And so in a request reply pattern.
- 49:05Yeah.
- 49:05So wherever you have a receive on the server side, you have a send on the client side and vice versa.
- 49:10So in a request reply pattern, the client is doing send a request and then receive to get the reply.
- 49:17In a server, you're doing receive a request and send the reply.
- 49:21In PubSub, you're just, you're only sending on the publisher side and on the subscriber side, you're only receiving.
- 49:27Nice.
- 49:27And when the way you set this, you basically choose these things is when you go to the context and you create the socket, you tell it what kind of pattern you're looking for.
- 49:35Is that where you specify that?
- 49:36Yeah.
- 49:36So ZMQ has a bunch of constants that identify socket types.
- 49:40So you'd use, when you create a socket, you always have to give it a single argument that is the socket type.
- 49:45So it'd be like ZMQ.pub for a publisher socket, ZMQ.sub for a subscriber socket, router, dealer, push, pull.
- 49:51Yeah.
- 49:52And that defines the messaging pattern of the underlying sockets.
- 49:55You also have some JupyterLab examples, which I guess we can link to as well, like some diagrams for that, right?
- 50:02Yeah.
- 50:02So the Jupyter protocol has a diagram of, a diagram that we maybe should redesign.
- 50:08Whoops.
- 50:08I didn't mean that far.
- 50:09There we go.
- 50:11It shows you what, basically what happens when you have one kernel and multiple front ends connected to it with the different socket types that we have in the Jupyter protocol.
- 50:19So the Jupyter kernel has two router sockets and a pub socket and a fully featured front end would have two dealer sockets or request sockets and a sub socket.
- 50:31It's just so much is happening below the scenes.
- 50:33I think getting your mind around these is really neat, but basically ZMQ is handling so much of this for everyone, right?
- 50:39It's handling all the, so we never care about, there's multiple peers connected.
- 50:43We don't need to deal with that.
- 50:45We have no connection events.
- 50:46Some folks working on different issues that causes headaches for them because there's some aspects of ZMQ, like the not guaranteed pub sub delivery.
- 50:55It's actually kind of a pain because we actually want all the messages.
- 51:00Yeah, yeah, of course.
- 51:02Is there any, you know, around a lot of libraries, there's stuff that like adds layers that does stuff.
- 51:07So like Flast extensions and stuff like that.
- 51:10Is there an extension that will like let you do reliable messaging that you can plug in on top of this or anything like that?
- 51:16Yeah.
- 51:17So if you look at the ZMQ guide, there are different patterns, some of which are basic uses of sockets.
- 51:22So there's no reason to build a, another layer of software on, in order to implement that, a simple ventilator pattern.
- 51:30But if you're talking about things like reliable messaging, there are some patterns in the guide and they, they have names.
- 51:37And so some people have written those protocols as standalone Python packages that say like implement this scheme on top of ZMQ that might have things like a message replay and, or, you know, election stuff.
- 51:52Like if you do Kubernetes things, they're often leader elections to allow you to scale and migrate things.
- 51:57So you can do that with a ZMQ applications.
- 52:00Yeah.
- 52:00And some of those reliable messaging things sound amazing.
- 52:02And I go, yeah, that's going to be great.
- 52:03But there's other drawbacks to those as well.
- 52:06Like poison messages.
- 52:07Like I got to make sure I send this, but the server, the client can't receive it.
- 52:11So they crash.
- 52:12So then I try to send it again.
- 52:13And you're just in these like weird loops and there's a lot of, they all have their challenges.
- 52:17Yeah.
- 52:17So another thing that I think would be interesting to touch on for our conversation, which we've spent so much time talking about all the programming patterns and stuff that I don't know.
- 52:26We have as much time anyway as we imagined, but building PyZMQ for basically to wrap this C library, right?
- 52:35This is some challenges you've had.
- 52:36It supports both CPython and PyPI.
- 52:39Sorry, PyPI.
- 52:40PyPI.
- 52:40Yeah.
- 52:40And whatnot.
- 52:41So maybe talk about some of the ways you did that.
- 52:44You had to do this like in the early days when there was Python 2 and 3.
- 52:48There's a lot of stuff going on, maybe pre-wheels, right?
- 52:50Yeah.
- 52:51So a few years pre-wheels.
- 52:52So with IPython and Jupyter, our target audience is pretty wide, right?
- 52:56We have a lot of people in education, a lot of students, a lot of people on Windows.
- 53:00A lot of those people don't even want to be programmers or care about like.
- 53:04Exactly.
- 53:04They just want this to work.
- 53:05And why won't this thing install?
- 53:06I just need to do this for my class or for my project.
- 53:09It needs to work, right?
- 53:10That kind of thing.
- 53:11Yeah.
- 53:11And so having a compiled dependency was a big deal for a lot of people.
- 53:18And so making binary releases as widely installable as possible was really important to us.
- 53:24And supporting as many Python implementations as possible was also important to us.
- 53:30So PyZMQ was originally written all in Cython, which is a wonderful library for this when you're interfacing with the C library, especially when you want to do things with the buffer interface.
- 53:42So when you have a C object and a Python object that represent the same memory, Cython is the best.
- 53:48And that's a lot of what we do for the zero copy stuff in PyZMQ.
- 53:54So when we were working on this, wheels didn't exist.
- 53:56Wheels being the binary version that you get from PyPI now.
- 53:59Yeah.
- 54:00So if you pip install something like PyZMQ, it doesn't compile it, right?
- 54:04You get a wheel and that just unzips it and it's really nice.
- 54:07But at the time there were only eggs.
- 54:08And there was a period of time when pip was taking over from easy install, which was eventually wonderful.
- 54:15But one of the drawbacks was during this time when pip was taking over, there was still no wheels.
- 54:21And so people had started shifting to pip because easy install did a lot of things that people don't like.
- 54:25But you had to use easy install if you wanted to get a binary, which means effectively, if you're on Windows, you had to use easy install.
- 54:32And so we had a really complicated, there actually used to be a big delay, might even still be there, in PyZMQ.
- 54:38No, this is definitely not.
- 54:40So there was a big delay in PyZMQ that if you ran setup.py, it would sleep for 10 seconds and show you a big message that says you might want to easy install.
- 54:47But now we're in a very different world, right?
- 54:49So even after wheels, it was a couple of years before we had many Linux wheels, right?
- 54:54There was a while before you were even allowed to make wheels for Linux.
- 54:57And now we're at a place where we've got wheels for ARM Macs and ARM Linux and a bunch of different Linux versions and Windows and everything.
- 55:09And it's a really different world.
- 55:12And a lot of things would be different if we were starting this project now.
- 55:14It would be easier if you started now, probably, right?
- 55:16Yeah, it would be a lot easier.
- 55:17We were getting in on some early stuff.
- 55:19But one of the wonderful things about Cython is if you're writing Cython, you're really writing C, right?
- 55:24If you're writing Cython code, it's generating a C program that calls the Python C API.
- 55:30Right.
- 55:30You write Python with a little typing stuff.
- 55:33It turns that into C and then compiles that to machine instructions, right?
- 55:37Like you're basically projecting C somehow.
- 55:39Yeah.
- 55:40You're basically writing C that looks like Python.
- 55:41The nice thing about C is that with directives and things, you can have one file that actually contains 10 different files.
- 55:50Because you could just turn off lines when it's compiling.
- 55:54And that means that it's much easier to write Cython code that supported Python.
- 55:59At the time, we were supporting Python 2.5 through 3.1.
- 56:02And with a single code base, we had no 2 to 3, none of that single code base in 2010 supporting Python 2.5 and Python 3.1 and everything in between.
- 56:11That's quite the accomplishment.
- 56:12And the tricks were all dealing with PyZMQ.
- 56:15We were early adopters a lot of the Python concepts, Python 3 concepts of we talk bytes.
- 56:20We don't talk str.
- 56:21Right.
- 56:21Disambiguate.
- 56:22We use bytes and Unicode everywhere.
- 56:24We never use the word str.
- 56:26Yeah.
- 56:27There's not necessarily that many differences between Python 2 and 3.
- 56:31Yeah.
- 56:31But you could make them different, right?
- 56:33But in a lot of the more modern Python 2, you could still be much closer to what eventually became Python 3, yeah?
- 56:40So I would say it was either 3.3 or 3.4.
- 56:44It became the norm to have single code base, single syntax, support 2.7.
- 56:50So drop support for 2.6, support 2.7.
- 56:52And I think 3.3 or above, or 3.4 and above, whenever they let you back in.
- 56:57Because then it became easier.
- 56:58What's the story with Python 2 now?
- 57:00Does it still support it?
- 57:01Just as of December, right?
- 57:03So Python 2 end of life was last December.
- 57:06Right.
- 57:07PyZMQ dropped support.
- 57:09The latest release requires Python 3.6, actually.
- 57:11Nice.
- 57:11Yeah.
- 57:12I feel like a lot of people are going to 3.6.
- 57:15Why did you guys choose 3.6?
- 57:16Is it f-strings or was it something else?
- 57:18It was actually the typing.
- 57:19All right.
- 57:20One of the main complaints about PyZMQ is it's so auto-generated and dynamically defined,
- 57:25right?
- 57:25Because we didn't just target multiple versions of Python.
- 57:28There's also multiple versions of LibZMQ.
- 57:29And that means that PyZMQ, what constants are defined is different depending on what version
- 57:36of LibZMQ is linked, which meant that a lot of the way it's written, a lot of static analysis
- 57:40fails.
- 57:41So when you're auto-completing based on static analysis, all the constants don't show up.
- 57:46And so that was an occasional focus.
- 57:49My auto-complete in PyCharm is not working.
- 57:51That's why I added the types was to allow static.
- 57:53Oh, yeah.
- 57:54That's fantastic.
- 57:55Does it do anything with what TypeShed does, where it's defining the structure in these stub
- 58:01files?
- 58:01So there's some type annotations in the pure Python code, but the relevant part was the stub
- 58:07files for the compiled part.
- 58:09Yeah.
- 58:10And if people haven't seen those stub files, those PYI files, it's a little like a C++ header
- 58:15thing where it has the definition, but then somewhere else is the implementation.
- 58:19of it.
- 58:19It's a little funky, but yeah, it's also useful for adding that in, right?
- 58:23So you could say, here's the structure, and we'll make that at runtime dynamically, but
- 58:27this is what you should think of it as, right?
- 58:29Yeah.
- 58:29Yeah.
- 58:29Very cool.
- 58:30Very cool.
- 58:31All right, Min, well, you know, I think we just honestly just scratched the surface.
- 58:34We could go on and on and on.
- 58:36But at the same time, I want to be respectful of your time.
- 58:39So maybe we should wrap it up on the main topic there.
- 58:43So I'll ask you the two questions on the way out.
- 58:46If you're going to write some code, if you're going to work on PYZMQ or something like that,
- 58:50what editor would you use?
- 58:52My favorite editor of all time is TextMate.
- 58:54Okay.
- 58:55But for various reasons, I don't use that anymore.
- 58:57It kind of, it hasn't kept up with activity and things, and I didn't feel sustainable anyway.
- 59:03So I haven't used it in a long time.
- 59:04I've tried pretty much everything.
- 59:06And I'm in a, I'm in constant search of the next TextMate.
- 59:10So right now I'm actually using Nova, the new editor from Panic.
- 59:14I was going to say, maybe Nova is your next TextMate.
- 59:16How about that?
- 59:17Like, I know if I say Visual Studio Code or PyCharm, people are like, oh yeah, I'm pretty sure I know what that means.
- 59:21Nova is pretty new.
- 59:23Tell folks about it.
- 59:24So Nova is a new text editor from Panic, one of the great Mac developers.
- 59:29Oh yeah, I use some of their apps, like Transmit for working with S3 files.
- 59:33They're really nice stuff, yeah.
- 59:35Yeah, so they do a great job designing things.
- 59:37And thanks to the recent work of the language server protocol and stuff, new editors are able to, there's a lot more shared infrastructure in editor features.
- 59:47So it's starting further from zero than it might otherwise be.
- 59:51But I'm not sure I could recommend it widely to Python developers.
- 59:55It's a bit of an early adopter situation.
- 59:57It is supposed to have Python support, but it's not specifically for Python, right?
- 1:00:02Yeah, no, it's a general purpose editor.
- 1:00:04And the target community is more in Mac developers, Ruby and web stuff.
- 1:00:08Yeah, I feel like it's pretty JavaScript friendly.
- 1:00:10Yeah.
- 1:00:11And maybe Mac developers as well, yeah.
- 1:00:12Yeah, and extensions are written in JavaScript.
- 1:00:14I've written a couple extensions to use the darker code formatter.
- 1:00:19Very cool.
- 1:00:21All right, well, that's quite neat.
- 1:00:23And I'm glad to hear that's working out for you.
- 1:00:26I've wanted to try it, but I just haven't.
- 1:00:28And then notable PyPI packages.
- 1:00:30I know you picked two that have some relation back to this challenge of building binary stuff and distributing it.
- 1:00:36Yeah.
- 1:00:37So up until December, there was exactly one computer in the world that could build PyZMQ releases.
- 1:00:42And now it's my laptop.
- 1:00:44And I finally solved that problem thanks to two wonderful packages.
- 1:00:47One is cibuildwheel, which is a more generally useful.
- 1:00:51If you have compiled Python packages, cibuildwheel is a wonderful thing for building and distributing all your wheels on all kinds of platforms.
- 1:00:59So now PyZMQ wheels are all built on GitHub actions, and I don't need to do anything other than tag a release.
- 1:01:04And it all happens magically.
- 1:01:06Nice.
- 1:01:06The other one that I wanted to highlight that probably fewer people know about that's related to cibuildwheel, because when you build a Python package that has an external dependency, there's an extra step to say, I built PyZMQ.
- 1:01:18I linked it against LibZMQ over here.
- 1:01:20But if somebody else installs that wheel as it is, it's not going to work because it's going to say, like, I don't have LibZMQ.
- 1:01:25So for a long time, there's a Mac thing called Delocate and a Linux one called auditwheel that say, look at the binaries in there and find them on your system, bring them in and update the linking to make sure they load.
- 1:01:38So the wonderful thing that I just found is someone created something called delvewheel, which is auditwheel or Delocate, but for Windows.
- 1:01:46And I don't understand anything about Windows.
- 1:01:48You go grab the DLLs and all that kind of stuff that have to be there and, you know, put it in the right location.
- 1:01:54Perfect.
- 1:01:54Yeah.
- 1:01:55And so for a long, long time, for, yeah, I guess 10 years plus, PyZMQ built LibZMQ on Windows as an extension.
- 1:02:03So it actually took the ZeroMQ sources and then just said, hey, I have a Python, this is a Python extension.
- 1:02:08Don't, you don't need to worry about the fact that it's actually a C++ library.
- 1:02:12Just pass it to distutils and compile it as an extension.
- 1:02:15And there were a lot of issues with that.
- 1:02:17You never got up good optimized results, but it worked most of the time.
- 1:02:21And that was the point.
- 1:02:22And it was a wonderful contribution from Brandon Rhodes that was a huge step in making PyZMQ installable.
- 1:02:28A lot more of the time was this bundling LibZMQ as an extension.
- 1:02:32But thankfully, finally got to the point where I almost never do that anymore.
- 1:02:37Nice.
- 1:02:38That's beautiful.
- 1:02:38Thanks to delvewheel.
- 1:02:39So delvewheel is my big one.
- 1:02:41Yeah.
- 1:02:41The CI Build Wheel one.
- 1:02:42It makes me happy to see the macOS Apple Silicon.
- 1:02:45Got a little checkbox there.
- 1:02:47That's what I'm doing or recording from on my machine over here.
- 1:02:50Got the Mac Mini one.
- 1:02:51And man, that is a sweet device.
- 1:02:53But you're a little bit back in like, oh, we don't have wheels for your system.
- 1:02:56Sorry.
- 1:02:57Yeah, I have.
- 1:02:58So I have two Mac Silicon wheels.
- 1:02:59One that I just finished last week with CI Build Wheel.
- 1:03:03That's a universal wheel.
- 1:03:05And then I have another like Mac ARM wheel targeting macOS 11 plus basically just for Homebrew Python
- 1:03:11on Homebrew Python 3.9 on ARM Macs.
- 1:03:14That's pretty specific.
- 1:03:16But yes, that's a thing I have actually.
- 1:03:18It's not an insignificant target.
- 1:03:20And that one I build on my wife's new laptop.
- 1:03:22Very cool.
- 1:03:23Very cool.
- 1:03:23So that's the only one that's not built on CI yet.
- 1:03:25Right.
- 1:03:25Awesome.
- 1:03:26Well, thank you so much for sharing all the stuff.
- 1:03:29And of course, all your work.
- 1:03:31I feel like I have a lot to go learn, but it's exciting stuff to be able to think about
- 1:03:35new networking ways of doing stuff with networking and Python.
- 1:03:39So thanks so much for that.
- 1:03:40And, you know, final call to action.
- 1:03:41People want to get started with this stuff.
- 1:03:43What would you tell them to do?
- 1:03:44I'd say read the guide.
- 1:03:45So if you're interested in ZeroMQ, think about building distributed applications and things,
- 1:03:49read the ZeroMQ guide, the whole thing.
- 1:03:51And I think it'll give you some new ideas.
- 1:03:53Even if you don't use ZeroMQ, it'll give you some new, good new ideas for how to think
- 1:03:56about this kind of application.
- 1:03:57Right.
- 1:03:57Like these design patterns that maybe are not so common, but like PubSub or whatever.
- 1:04:02Yeah.
- 1:04:02Awesome.
- 1:04:03Well, thanks again for being on the show.
- 1:04:04It was great to chat with you.
- 1:04:05Yeah.
- 1:04:05Thanks so much.
- 1:04:06You bet.
- 1:04:06Bye.
- 1:04:07This has been another episode of Talk Python To Me.
- 1:04:10Our guest on this episode was Min Ragan-Kelley.
- 1:04:12And it's been brought to you by Linode and Mido.
- 1:04:15Simplify your infrastructure and cut your cloud bills in half with Linode's Linux virtual machines.
- 1:04:20Develop, deploy, and scale your modern applications faster and easier.
- 1:04:23Visit talkpython.fm/Linode and click the create free account button to get started.
- 1:04:29Do you feel like you're stumbling around trying to work with pandas within your Jupyter notebooks?
- 1:04:33What if you could work with data frames visually just like they were an Excel spreadsheet, but
- 1:04:38have it write the Python code for you?
- 1:04:40With Mido, you can.
- 1:04:41Check them out at talkpython.fm/Mido.
- 1:04:45M-I-T-O.
- 1:04:45Want to level up your Python?
- 1:04:47We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:04:51Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:04:57And best of all, there's not a subscription in sight.
- 1:04:59Check it out for yourself at training.talkpython.fm.
- 1:05:02Be sure to subscribe to the show, open your favorite podcast app, and search for Python.
- 1:05:07We should be right at the top.
- 1:05:08You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:05:13and the direct RSS feed at /rss on talkpython.fm.
- 1:05:18We're live streaming most of our recordings these days.
- 1:05:21If you want to be part of the show and have your comments featured on the air,
- 1:05:25be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:05:29This is your host, Michael Kennedy.
- 1:05:31Thanks so much for listening.
- 1:05:32I really appreciate it.
- 1:05:33Now get out there and write some Python code.
- 1:05:35I'll see you next time.
- 1:05:56Thank you.