forked from mikeckennedy/talk-python-transcripts
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path369-getting-lazy-with-python-imports-and-pep-690.vtt
More file actions
5377 lines (2689 loc) · 96 KB
/
369-getting-lazy-with-python-imports-and-pep-690.vtt
File metadata and controls
5377 lines (2689 loc) · 96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
WEBVTT
00:00:00.000 --> 00:00:04.000
- Hello YouTube, hello Barry, Erman, Carl.
00:00:04.000 --> 00:00:04.960
Welcome to the show.
00:00:04.960 --> 00:00:07.400
- Hey, hello.
00:00:07.400 --> 00:00:10.520
- It's wonderful to have you all here.
00:00:10.520 --> 00:00:14.800
I'm very excited about the work that you're all doing
00:00:14.800 --> 00:00:17.160
around Python performance.
00:00:17.160 --> 00:00:19.520
We're gonna focus on imports and this pep
00:00:19.520 --> 00:00:21.760
that you three proposed today,
00:00:21.760 --> 00:00:24.520
but it's really just the tip of the iceberg
00:00:24.520 --> 00:00:26.840
in terms of a bunch of cool stuff that's going on.
00:00:26.840 --> 00:00:28.720
So I'm very excited to dive into that
00:00:28.720 --> 00:00:29.840
with the three of you.
00:00:29.840 --> 00:00:31.300
Now, before we get to it though,
00:00:31.300 --> 00:00:33.700
let's just do a quick round of introductions.
00:00:33.700 --> 00:00:35.300
Barry, you've been on the show before
00:00:35.300 --> 00:00:40.300
talking about 1994 Python stuff and other things.
00:00:40.300 --> 00:00:44.380
So maybe just a quick introduction for yourself.
00:00:44.380 --> 00:00:46.860
- Yeah, Barry Warsaw.
00:00:46.860 --> 00:00:50.900
So I'm still here, still hanging around, I guess.
00:00:50.900 --> 00:00:56.700
You know, I should mention, you know,
00:00:56.700 --> 00:00:59.120
as we get into this PEP though,
00:00:59.120 --> 00:01:00.720
I'm really just the sponsor.
00:01:00.720 --> 00:01:04.920
I gotta hand a lot of thanks to Hermon and Carl
00:01:04.920 --> 00:01:06.360
for doing all the work on it.
00:01:06.360 --> 00:01:13.120
Most peps these days require a core developer to sponsor.
00:01:13.120 --> 00:01:17.280
So I'm just super interested in the topic.
00:01:17.280 --> 00:01:19.560
I think it's a super clever approach.
00:01:19.560 --> 00:01:24.320
And so I think it'll help specific needs
00:01:24.320 --> 00:01:25.160
that I have at work.
00:01:25.160 --> 00:01:27.760
And so I was really eager to...
00:01:27.760 --> 00:01:31.640
eager to sponsor it.
00:01:31.640 --> 00:01:34.640
- Yeah, fantastic.
00:01:34.640 --> 00:01:36.440
All right, very cool.
00:01:36.440 --> 00:01:37.600
Hermann, how about you?
00:01:37.600 --> 00:01:45.040
- Well, I'm just working at Meta with Carl
00:01:45.040 --> 00:01:50.040
and I wrote the initial approach for Lazy Imports
00:01:50.040 --> 00:01:52.020
and well, here I am.
00:01:52.020 --> 00:01:54.320
- Right on.
00:01:56.080 --> 00:01:58.340
- Carl, quick introduction.
00:01:58.340 --> 00:02:04.440
- I've been around the Python community for a while.
00:02:04.440 --> 00:02:08.080
I think the first maybe semi-notable thing that I did
00:02:08.080 --> 00:02:12.760
was write the first version of pip uninstall back in 2009.
00:02:12.760 --> 00:02:16.040
That led to being a maintainer of pip and virtualen
00:02:16.040 --> 00:02:19.840
for a while, and I worked on the Django core team
00:02:19.840 --> 00:02:21.080
for a while.
00:02:21.080 --> 00:02:24.320
And so yeah, I've been doing Python things for a long time
00:02:24.320 --> 00:02:25.840
and I've been working at Meta since--
00:02:25.840 --> 00:02:27.840
- Those are a lot of big projects there.
00:02:27.840 --> 00:02:30.600
- Yeah, they're projects that I was using
00:02:30.600 --> 00:02:32.560
and so I was interested in working on them
00:02:32.560 --> 00:02:34.500
and same goes for Python itself.
00:02:34.500 --> 00:02:36.760
- Yeah, absolutely.
00:02:36.760 --> 00:02:37.880
Sorry, I kind of cut you off there.
00:02:37.880 --> 00:02:40.640
You said working at Meta since 2016, is that what you said?
00:02:40.640 --> 00:02:43.520
- Yeah, I've been working at Meta since 2016.
00:02:43.520 --> 00:02:47.360
Working mostly on how Meta uses Python,
00:02:47.360 --> 00:02:48.680
how Instagram uses Python.
00:02:48.680 --> 00:02:50.800
- Yeah.
00:02:50.800 --> 00:02:53.400
There've been some really cool looks inside what's going on,
00:02:53.400 --> 00:02:57.440
especially at Instagram there with some of the typing talks
00:02:57.440 --> 00:03:02.440
that Lucas gave, as well as sort of suspending
00:03:02.440 --> 00:03:05.160
the garbage collector for other various things.
00:03:05.160 --> 00:03:07.880
And a lot-- or was that you guys?
00:03:07.880 --> 00:03:10.480
I think it was, right?
00:03:10.480 --> 00:03:11.480
Yeah, we had a whole--
00:03:11.480 --> 00:03:13.240
A lot of stuff coming out of there.
00:03:13.240 --> 00:03:13.920
A whole saga.
00:03:13.920 --> 00:03:15.380
We turned off the garbage collector,
00:03:15.380 --> 00:03:16.960
turned back on the garbage collector.
00:03:16.960 --> 00:03:19.840
There's been multiple blog posts along the way
00:03:19.840 --> 00:03:24.240
explaining why we've done each of those silly sounding things.
00:03:24.240 --> 00:03:25.920
>> Sure, I mean, they seem insane.
00:03:25.920 --> 00:03:28.360
Well, a lot of the projects that,
00:03:28.360 --> 00:03:29.880
a lot of the stuff that's coming out here,
00:03:29.880 --> 00:03:32.080
it has to do with the Cinder project, right?
00:03:32.080 --> 00:03:35.200
>> Yeah.
00:03:35.200 --> 00:03:37.160
>> And you may be, I don't know who's best
00:03:37.160 --> 00:03:38.560
to give the introduction for Cinder,
00:03:38.560 --> 00:03:43.560
but Cinder's a really cool project about, you know,
00:03:43.560 --> 00:03:46.280
taking a whole bunch of optimizations
00:03:46.280 --> 00:03:48.160
and specializations you all have done
00:03:48.160 --> 00:03:50.920
and sort of sharing that back with the community
00:03:50.920 --> 00:03:51.920
a little bit, right?
00:03:51.920 --> 00:03:57.960
- Yeah, so we started Cinder in I think 2017 or 2018.
00:03:57.960 --> 00:03:59.680
There was actually two projects
00:03:59.680 --> 00:04:01.080
kind of started simultaneously.
00:04:01.080 --> 00:04:04.280
We realized around that time that the trajectory
00:04:04.280 --> 00:04:09.040
for Instagram's kind of server footprint
00:04:09.040 --> 00:04:11.600
was not really sustainable just in terms of how much
00:04:11.600 --> 00:04:14.640
server CPU time we were spending running Python code.
00:04:14.640 --> 00:04:16.440
And so we kicked off two projects.
00:04:16.440 --> 00:04:19.380
One was called, it's now called Sky Bison.
00:04:19.380 --> 00:04:23.220
It was like a ground up rewritten Python interpreter,
00:04:23.220 --> 00:04:27.140
you know, using all the modern dynamic language VM ideas,
00:04:27.140 --> 00:04:28.580
like a moving garbage collector
00:04:28.580 --> 00:04:30.180
and all these different things.
00:04:30.180 --> 00:04:32.740
We weren't the first people to try that.
00:04:32.740 --> 00:04:36.940
And we also weren't the first people to fail.
00:04:36.940 --> 00:04:39.720
That project was wound down last year,
00:04:39.720 --> 00:04:41.380
just weren't able to get the performance,
00:04:41.380 --> 00:04:43.740
particularly trying to emulate compatibility
00:04:43.740 --> 00:04:46.020
with the C API and all the C extensions,
00:04:46.020 --> 00:04:50.180
which is the same reason many prior rewrite efforts
00:04:50.180 --> 00:04:51.220
didn't go very far.
00:04:51.220 --> 00:04:53.860
So at the same time as we'd kicked off SkyBison,
00:04:53.860 --> 00:04:55.740
we had sort of kicked off what we thought
00:04:55.740 --> 00:04:58.320
was a short-term project of just like,
00:04:58.320 --> 00:05:00.060
what can we squeeze out of CPython?
00:05:00.060 --> 00:05:02.220
Where can we get a little more performance out of it?
00:05:02.220 --> 00:05:04.000
And that turned into Cinder,
00:05:04.000 --> 00:05:06.300
and then ended up kind of becoming our primary approach
00:05:06.300 --> 00:05:07.640
to Python performance.
00:05:07.640 --> 00:05:13.660
- Isn't it always the story that the C interop stuff
00:05:13.660 --> 00:05:16.900
is the big sticking point here.
00:05:16.900 --> 00:05:17.740
- Yeah.
00:05:17.740 --> 00:05:19.100
- I mean, Barry, you must have seen a whole bunch
00:05:19.100 --> 00:05:21.980
of examples from a core dev perspective.
00:05:21.980 --> 00:05:23.140
All right.
00:05:23.140 --> 00:05:23.980
- Yeah.
00:05:23.980 --> 00:05:24.800
- We could just change the guild,
00:05:24.800 --> 00:05:28.580
but the C API, we could just, but you know.
00:05:28.580 --> 00:05:29.620
- Yeah, yeah.
00:05:29.620 --> 00:05:33.820
I mean, it's both, you know, Python's, you know,
00:05:33.820 --> 00:05:35.780
advantage, you know, pros and cons, right?
00:05:35.780 --> 00:05:39.500
Like the approachability and the usability
00:05:39.500 --> 00:05:44.260
the C API has led directly to the incredible ecosystem
00:05:44.260 --> 00:05:47.120
of extension modules, right?
00:05:47.120 --> 00:05:50.980
But those also are also the hindrance for moving,
00:05:50.980 --> 00:05:52.860
you know, ahead in a revolutionary way
00:05:52.860 --> 00:05:55.900
with the interpreter, I think, you know.
00:05:55.900 --> 00:05:57.900
- Right, you kind of got to live within the box,
00:05:57.900 --> 00:06:01.020
with the walls that are put up by those constraints.
00:06:01.020 --> 00:06:02.340
But I do think it's super important.
00:06:02.340 --> 00:06:03.940
You know, a lot of people would maybe just think,
00:06:03.940 --> 00:06:04.820
well, we'll just get rid of it.
00:06:04.820 --> 00:06:06.380
Like, let's just try to move beyond it.
00:06:06.380 --> 00:06:09.800
but when you hear people say that Python is slow
00:06:09.800 --> 00:06:11.380
or it has these other problems,
00:06:11.380 --> 00:06:13.200
so often what you'll see as well,
00:06:13.200 --> 00:06:15.820
and what I did was I did a for loop in Python
00:06:15.820 --> 00:06:18.160
and did some math and that was slow.
00:06:18.160 --> 00:06:19.860
It's like, well, but if a thing is slow,
00:06:19.860 --> 00:06:22.180
so often that gets rewritten in C
00:06:22.180 --> 00:06:23.940
and then all of a sudden it's faster than,
00:06:23.940 --> 00:06:26.180
I don't know what Java or whatever else it is
00:06:26.180 --> 00:06:28.020
they're trying, you know, Node,
00:06:28.020 --> 00:06:29.120
but they're trying to compare it to.
00:06:29.120 --> 00:06:32.320
And so there's this kind of crazy switch
00:06:32.320 --> 00:06:35.800
that gets flipped for like really high performance.
00:06:35.800 --> 00:06:39.920
and then maybe acceptable most of the time performance.
00:06:39.920 --> 00:06:43.920
And it's this interop with C that is the thing,
00:06:43.920 --> 00:06:45.080
that's the escape hatch.
00:06:45.080 --> 00:06:51.200
- Yeah, and LinkedIn, we don't have the same kind
00:06:51.200 --> 00:06:55.080
of workloads that they have at Meta,
00:06:55.080 --> 00:06:58.480
but we've done analysis.
00:06:58.480 --> 00:07:00.480
One of the reasons why I was particularly interested
00:07:00.480 --> 00:07:02.800
in this project is because we write a lot
00:07:02.800 --> 00:07:04.840
of our CLIs in Python.
00:07:04.840 --> 00:07:09.840
And so, we've had proponents of other languages say,
00:07:09.840 --> 00:07:11.920
complain, "Hey, Python's really slow.
00:07:11.920 --> 00:07:13.600
"I write a CLI in Python."
00:07:13.600 --> 00:07:16.080
And then it takes a long time to start up.
00:07:16.080 --> 00:07:18.520
But if you actually do the analysis,
00:07:18.520 --> 00:07:22.520
what you find is that people are not writing,
00:07:22.520 --> 00:07:25.880
a lot of the internal libraries that those CLIs import
00:07:25.880 --> 00:07:27.620
do things like they go hit the network
00:07:27.620 --> 00:07:29.280
and they go try to do the service
00:07:29.280 --> 00:07:31.560
and create really expensive resources
00:07:31.560 --> 00:07:33.080
at module scope time, right?
00:07:33.080 --> 00:07:35.440
So like those are the types of analysis
00:07:35.440 --> 00:07:36.800
that you really need to do to say,
00:07:36.800 --> 00:07:39.600
is it really Python or is it the way we use Python?
00:07:39.600 --> 00:07:42.340
You know, for us, it's a little of both for sure,
00:07:42.340 --> 00:07:46.200
but often it's sort of the way Python is being used
00:07:46.200 --> 00:07:50.080
in a non-idiomatic or not in the highest performance way.
00:07:50.080 --> 00:07:50.920
Right?
00:07:50.920 --> 00:07:52.960
So a little bit of rewrite of some internal code
00:07:52.960 --> 00:07:54.080
can get you a long way.
00:07:54.080 --> 00:07:56.400
- It sure can.
00:07:56.400 --> 00:07:58.840
I also want to just give a quick shout out
00:07:58.840 --> 00:08:02.240
back on episode 347, I talked with Dino Vila
00:08:02.240 --> 00:08:03.900
like all about the Cinder project.
00:08:03.900 --> 00:08:04.840
So if you want to check that out,
00:08:04.840 --> 00:08:07.280
they can definitely go find that.
00:08:07.280 --> 00:08:11.280
But there's a lot of cool things that were taken out of Cinder
00:08:11.280 --> 00:08:12.440
and are being proposed.
00:08:12.440 --> 00:08:17.200
And I guess that sort of brings us to our main topic for today
00:08:17.200 --> 00:08:21.200
is imports and the PEP 690 that you all have proposed,
00:08:21.200 --> 00:08:23.280
which I am also excited for.
00:08:23.280 --> 00:08:26.640
I think imports are one of those things that
00:08:26.640 --> 00:08:29.560
are a little bit mysterious to people
00:08:29.560 --> 00:08:33.240
because they have conceptions coming from other languages,
00:08:33.240 --> 00:08:35.560
especially compiled languages that are very different
00:08:35.560 --> 00:08:37.020
than what the reality is.
00:08:37.020 --> 00:08:41.240
So I do want to talk a whole bunch
00:08:41.240 --> 00:08:42.560
about what you're offering here,
00:08:42.560 --> 00:08:46.720
what you're proposing here in this lazy imports pep.
00:08:46.720 --> 00:08:49.120
But before we do, let's maybe just set the stage
00:08:49.120 --> 00:08:54.080
with what happens when I write import requests
00:08:54.080 --> 00:08:56.040
or I write import FastAPI,
00:08:56.040 --> 00:08:59.800
or even something built in like import collections?
00:08:59.800 --> 00:09:02.720
Who wants to take this one?
00:09:02.720 --> 00:09:03.880
Tell people what really happens
00:09:03.880 --> 00:09:06.440
when the import statement runs, is there.
00:09:06.440 --> 00:09:11.240
- I don't know.
00:09:11.240 --> 00:09:14.420
I guess I'll say,
00:09:14.420 --> 00:09:19.160
there was a time where the import system
00:09:19.160 --> 00:09:21.200
for Python was written in C
00:09:21.200 --> 00:09:23.560
and it was even more mysterious, I think at that point.
00:09:23.560 --> 00:09:26.560
And I don't remember when it was,
00:09:26.560 --> 00:09:31.140
but Brett Cannon rewrote the import system into Python.
00:09:31.140 --> 00:09:32.900
And then about that time,
00:09:32.900 --> 00:09:36.260
I sort of went through sort of line by line
00:09:36.260 --> 00:09:38.760
and tried to understand, I mean, because right,
00:09:38.760 --> 00:09:42.620
like imports are probably one of the oldest features
00:09:42.620 --> 00:09:43.460
of Python, right?
00:09:43.460 --> 00:09:44.340
They've been around so long
00:09:44.340 --> 00:09:47.220
and there's so many corner cases, right?
00:09:47.220 --> 00:09:51.700
It's a very complex system and has lots of features,
00:09:51.700 --> 00:09:56.980
lots of hooks, lots of places where you can hook in your own behavior and really
00:09:56.980 --> 00:09:59.260
understanding how all of that fits together.
00:09:59.260 --> 00:10:04.140
I think I probably took a couple of months to sort of walk through the entire
00:10:04.140 --> 00:10:05.580
system line by line and document it.
00:10:05.580 --> 00:10:11.500
Fortunately now, if you go to the language reference guide, I think it goes into all
00:10:11.500 --> 00:10:14.180
the gory detail about how imports work.
00:10:14.180 --> 00:10:19.660
Everything from namespace packages to, you know, concrete packages and all the file
00:10:19.660 --> 00:10:23.900
system hooks and meta path hooks and blah, blah, blah, blah.
00:10:23.900 --> 00:10:24.400
Right?
00:10:24.400 --> 00:10:26.900
So yeah.
00:10:26.900 --> 00:10:29.940
Yeah, definitely a lot going on.
00:10:29.940 --> 00:10:32.660
I think the key thing, though, that you are hinting at there
00:10:32.660 --> 00:10:37.020
is this is a runtime type of behavior.
00:10:37.020 --> 00:10:39.380
This is a runtime experience.
00:10:39.380 --> 00:10:40.700
Carl, you're shaking your head.
00:10:40.700 --> 00:10:42.500
What would you want to add to this?
00:10:42.500 --> 00:10:44.580
What Barry was saying here.
00:10:44.580 --> 00:10:48.020
Well, yeah, I mean, I think what a lot of people don't--
00:10:48.020 --> 00:10:51.380
I mean, maybe don't consciously realize initially
00:10:51.380 --> 00:10:55.580
about imports is that it's just executing some more code.
00:10:55.580 --> 00:10:58.740
So it's like, you can almost think of imports in Python
00:10:58.740 --> 00:11:00.780
as like syntax sugar for a function call
00:11:00.780 --> 00:11:04.180
where the body of the function is the module level code
00:11:04.180 --> 00:11:07.540
of some module and the return value is the module object
00:11:07.540 --> 00:11:10.080
that you end up getting back from the import.
00:11:10.080 --> 00:11:10.940
But like it really is,
00:11:10.940 --> 00:11:14.580
it's just causing some more code to be executed
00:11:14.580 --> 00:11:17.820
where the result is a module object that has stuff on it
00:11:17.820 --> 00:11:19.100
that you can use.
00:11:19.100 --> 00:11:21.500
And one of the consequences of that
00:11:21.500 --> 00:11:25.860
is that literally anything can happen when you import a module.