PyCascades 2021 参会总结

关于这篇文章到底用中文还是英文写,其实我犹豫了一会,但最终还是决定用中文,毕竟也没人会写一篇关于 PyCascades 的中文文章了。

首先,PyCascades 是什么?用官网的话说:

PyCascades is a regional PyCon in the Pacific Northwest, celebrating the west coast Python developer and user community. Our organizing team includes members of the Vancouver, Seattle, and Portland Python user groups.

简单来说 PyCascades 就是一个区域性的 PyCon。但由于许多 Python 大佬聚居于此,其影响力比其它区域性 PyCon 大很多。

我在 PyCascades 2021 给了一个演讲 “Let’s Rethink Debugging”:

下面就聊聊我从申请到参加 PyCascades 的经历。

申请

去年(2020)十月,PyCascades 官推宣布了 2021 年会议将在线上举办。当时 Cyberbrain 的第一个小版本已经接近完成,我正想找个地方宣传,于是就锁定了 PyCascades。彼时我只知道 PyCascades 有着不错的影响力,而对整个申请流程毫无概念。然后就发现想要演讲,得先提交一个 proposal。和 PyCon China 不同,PyCascades 有着严格的筛选机制。你得提交一个描述演讲内容的 proposal 给组委会审核,择优录取,所以并非报名了就能去讲。根据这份 slide 里提到的数据,PyCascades 2018 和 2019 年的演讲接收率(acceptance rate)都不超过 10%,可见筛选之严苛。

我从来没写过 proposal,完全不知道要怎么下手。好在官方提供了一些参考资料,我就把它们看了一遍。其中值得一提的是这个 repo,里面收集了过往被接受和拒绝的一些 PyCon proposals,很有参考价值。由于 19 年在 PyCon China 讲过,演讲的前半部分我基本直接照搬,力气主要花在写后半部分 Cyberbrain 的原理介绍和展望。10 月下旬,我花四个晚上完成了 proposal 的初稿,接着就开始找人审阅。

找谁呢?我想起之前看过的那些资料。既然作者有心教人写 proposal,那么应该也会愿意帮忙审阅吧。于是我就给两位作者同时也是圈内大佬 Allison KapturBrandon Rhodes 发了邮件。说来也巧,我在调研 Cyberbrain 时仔细阅读了 Allison 写的那篇 A Python Interpreter Written in Python,实现过程中也一直在参考文中提到的 byterun 项目,可以说渊源颇深。邮件发出后好多天也不见回信。我不想傻等,于是就推特私信了 PyCascades 的组织者 Nina。Nina 在一直是 Python 圈的活跃分子,以前也做过关于 debugging 的演讲,我觉得找她还挺合适的。很快有了回信:

This year I'm co-organized for PyCascades, so I currently have my hands full. Fortunately we have a team of volunteers that are providing speaker mentorship for the conference.

Would it be OK if I introduced you to one of our speaker mentors? If yes, just let me know which e-mail address is best for you.

我惊了,原来还有志愿者吗?那还用说,当然是赶快给我安排一个。于是,同为 Googler 的 Chris Wilcox 就成了我的 mentor。几乎是同时,Allison 和 Brandon 也回信了。于是接下来直到提交截止日期我几乎一直在改 proposal,成品就是上一篇文章中的那样。以后有空我可以专门聊聊怎么写 proposal,今天先略过了。

会前准备

Proposal 幸运地被接收了。感情上,我终于放下心来,然而理智上我从来就不觉得会被据——要是这都被拒了,被接收的那些不得上天?毕竟我也不是没看往届演讲。

于是就开始写 slide。我报名的是 recorded talk,也就是要提前录好。二月份提交才截止,理论上不用着急,但我想先试讲一次。我迅速搞定 slide,并联系了本地 Python meetup 的组织者,在 1 月 5 号晚上做了一次远程演讲。虽说只有二十多人参加,但反响还不错,这让我基本确信了这会是一个好的演讲。

到这步还不能直接录制,组委会要审核 slide 是否符合 Code of Conduct,并做一个 tech check。Tech check 委托了 Next Day Video(一个专业做会议视频的公司)进行,目的是为了确保讲师能够录制出效果足够好的视频。我先通过他们的系统预约,然后顺利进行并通过了 tech check。过程中,Next Day Video 的人还教了我怎么正确使用麦克风——原来我之前一直都摆错了方向导致无法获得最佳音效。果然这就是专业人士吗🤯。然后就是一些邮件+notion文档(1, 2, 3),里面说明了参会和录制的各种要求,详细到视频的 bit rate 和 frame rate 都有规定。里面还推荐了一些录制用的软件。我最后采用的是 Apowersoft Free Online Screen Recorder。CoC review 通过之后,终于可以录制了。

算上 PyCon China 2020 的闪电演讲,这还只是我第二次录视频。我发现,录视频比现场演讲累太多了。可能有人会觉得,现场演讲多紧张啊要面对那么多观众,提前录的话讲坏了大不了重录或者剪辑嘛。但这也正是录视频累人的地方

  • 没有现场观众,你无法得到肾上腺素或者其它什么激素的刺激,而这些刺激能极大缓解疲劳。
  • 你不太会在意现场演讲中的口误、结巴等不完美的地方,因为你在面对别人讲话,而讲话时自然不会在意这些。
  • 提前录制则不同,首先你是自说自话,会对各种错误非常敏感。其次你有重录的机会,导致容易吹毛求疵。

反正我第一次试着完整录,25 分钟,讲完就不行了。那天剩下的时间我几乎没办法干任何事,感觉整个人都被榨干了。自然效果也非常差,后半段可以明显感觉到讲师很疲惫。于是接下来的几天继续录,不算中途就抛弃掉的,大概第三遍的时候我觉得差不多了。我把视频传给了 Next Day Video,几天之后被告知没有问题。接下来就一身轻松等待会议开幕了。

Day 1

主办方在周五安排了一个线上的 social 活动,使用的平台是 SptialChat。这个产品还挺有意思的。进去之后有一个类似剧场的空间,你可以在空间中拖动自己的头像,模拟在物理空间中走动的感觉。当你把头像移到一群人旁边就可以听到他们讲话,并且距离越近听得越清晰。一开始我只是听,后来 Anthony Shaw 过来和我打招呼,我也就顺势混入那一群人瞎聊起来。我说我感觉录视频好累,Anthony 和另一个讲师说他们也发现录视频会让自己要求变高,到最后受不了只能把讲稿完全写下来。

主办方还找了个 DJ 在台上打碟。打碟的大叔全程也不和人交流,感觉很沉醉其中。

Day 2

周六是正式会议,九点半开场。然而我七点就起床看了场球,还输了。郁闷了一阵想起来还要开会,点进去发现 Guido、Brett 等几位大佬已经开始聊天了。随后一直听,并反复在 recorded talk 和 live talk 之间切换,因为 PyCascades 的两个 track 是同时进行的。值得一看的演讲是 Your Name Is Invalid!,大概就是讲处理各种语言文本时各种可能出的问题,比如有的语言中大小写字母不是一一对应的。最后的结论是 "Don't assume anything"。这让我想起以前看过的讲时区处理的视频,想用代码囊括复杂多样的世界,何其难也。

会议之前若干周,主办方在 Slack 上开了几个私密频道给讲师,当作后台使用。会议当天,Next Day Video 的人会在后台联系讲师做 tech check——是的,为了确保万无一失,当天还有 tech check。我的演讲在下午 1:55,大概 1 点做 tech check。工作人员说声音没问题,但让我把后面的百叶窗拉上一点不让太亮了,并且还问能不能把摄像头放低一点。这里要解释一下,我用的是一个台式机连接大显示器,摄像头是放在显示器上面的,这样就造成了一种俯视的感觉。为了解决视角问题,我只能找两个纸巾盒子摞起来,把摄像头放在上面,这样勉强和我的头平齐。我还挺好奇其它人都是怎么做的。

临近演讲时间,我登入 Next Day Video 的讲师专用后台。和主持人对了一次词,包括要怎么称呼和介绍我。这次会议我统一跟人说叫我"laike"了,毕竟称呼不重要。我之前以为开场介绍过后就可以退出,没想到还得一直待在后台。我只能找了一台笔记本打开听自己的演讲。他们强调说视频的播放有一段时间的延迟,但是我听着听着就忘了。临近结尾,我看到 slack 上有消息,是主持人让我马上过来说结束语。也就是说,我需要在视频没结束的时候就去讲,因为有延迟,观众看到的就是视频一结束,我和主持人马上出镜。我手忙脚乱地坐回屏幕前,调整摄像头,讲了一句话才发现自己处于 mute 状态,又赶紧 unmute。这状况频出的直播大概会成为未来的美好回忆吧。

后台状况不提,演讲效果还是不错的。听众不出意外地被 Cyberbrain demo 震住,当然这也在意料之中。我边听边打字回答听众问题,十分愉快。

有一些听众希望在 console 和 notebook 里使用。我只能说,希望未来可以实现吧。最令我高兴的还是看到 Łukasz 说他被 sold 了,虽然他后来也没有去 star 😢

之后就是在 Q & A 房间继续回答问题。有人问这个项目和 Google 有没有关系,我就非常想笑,怎么可能有关系啊。还有人问能否用 Cyberbrain profile 慢代码,我也只能承认不行。不管问题如何,能有机会和听众聊 Cyberbrain 就足够让我开心了。

Day 3

Day 3 是 sprints,我没参加只是去看了一眼,感觉线上做 sprints 还是有点奇怪。以后线下一定要参加一回。

大概就是这样。全程下来要问我哪一点印象最深,不是具体的 talk,也不是自己的第一次英文演讲,而是主办方那惊人的专业性。会前会中已经讲过,但这种专业性甚至延续到了会后。当他们把演讲视频上传到了 YouTube,我发现我在视频里有露脸。别不在意这个,但我并不想。正当我烦恼要怎么联系主办方解释自己的苦衷,甚至已经开始在脑中构思措辞时,我接到了一封邮件。这是一封确认视频有没有问题的邮件。我喜出望外,便在回复里说明了需求。于是他们迅速撤下视频,两天后剪辑完成又重新发布。这种受尊重的感觉实在太好了。我愿称 PyCascades 是我参与过的最专业的技术大会。

Let’s Rethink Debugging

本文是我在 PyCascades 2021演讲的 proposal。虽说是 proposal,却是以接近博文的风格写作的(毕竟我只会这种风格。。),所以就直接放出来水一篇了。对应的 slide 在这里。下一篇文章我会聊聊参加 PyCascades 的经历。

This is my talk proposal for PyCascades 2021. Even though it's a proposal, it reads very much like an article, so I just post it here.

Abstract

As programmers, we do debugging almost every day. What are the major options for debugging, what advantages and disadvantages do they have? We'll start the talk by giving the audience an overview of the history of debugging and existing tools so they know how to pick from them.

Then, we'll help the audience gain a deeper understanding of what debugging is really about, and talk about two pain points with existing solutions. We'll introduce a novel approach to solve these pain points, with basic introduction to bytecode tracing so the audience can learn this useful technique.

Finally, we'll look into the future and talk about why it's important to be more innovative. We hope that by listening to this talk, the audience can be more open-minded thinking about debugging, and programming as a whole.

No specific knowledge required, but basic experience with debugging would be helpful.

Description

Here is a detailed description of each part.

Part 1: What debugging is really about?

Broadly speaking, a Python program can have four types of errors:

  • Syntax Error
  • Exits abnormally (e.g. unhandled exceptions, killed by OS)
  • The program can run, but gives wrong results
  • Gives correct results, but consumes more resources than expected (e.g. memory leak)

Among which, the third type of error is the most common, and also where programmers spent most of their time debugging. In this talk we focus on this type of error, aka "A Program can run, but gives wrong results".

I'll let the audience recall how they usually do debugging. It's not hard to spot that, no matter what approach we take, we're trying to answer one question:

What is the root cause of the wrong value?

This sounds straightforward, but it is vital that we realize it before going into the later sections.

Part 2: Retrospect the history of debugging

In the early days of programming, debugging meant dumping data of the system or output devices - literally printing, or displaying some flashy lights if there's an error. A very patient programmer then would go step-by-step through the code, reading it to see where the problem may be.

Then, in the 70s and 80s, the idea of "debugging software" came along, and people started to build command-line debuggers like gbx and GDB. Since then, despite new features like breakpoint, reverse debugging and graphical interface were added, the way people use debuggers stays pretty much the same: step through the program and look around.

Today, print, logging, and debugger remain to be the major ways for debugging, each with its advantages and drawbacks:

  • print:
    • Advantages: available out-of-the-box, clean information, does not affect program execution.
    • Drawbacks: requires familiarity with code, needs tweaking repeatedly, lack of context, hard to manage output.
  • Logging:
    • Advantages: configurable, easy to manage output (e.g. Sentry), richer context (lineno, filename, etc).
    • Drawbacks: configuration is not easy, requires familiarity with code, hard to search what you need, context still not enough.
  • Debugger:
    • Advantages: powerful, does not require familiarity with code, richest context to help identify problems.
    • Drawbacks: not always available, decent learning curve, can't persist output, needs human interaction.

Yet, with all these options, debugging is still hard sometimes. We'll see why in the next section.

Part 3: Let's rethink debugging

There are two pain points with existing debugging solutions:

  • There is no tool that is as easy-to-use as a print, yet provides rich information like a debugger.

    Tool Effort required Information provided
    print low simple
    logging medium medium
    debugger high rich
    ? low rich
  • Existing tools only give clues, without telling why.

    This is a bigger (yet hidden) problem.

    In the first part we talked about the goal for debugging, which is finding out the root cause of the wrong value. Let's use debugger as an example to recall how we usually debug. Let's say you're debugging a program, where c has an unexpected value:

    c = a + b  # c should be "foo", but instead is "bar"
    

    Here are the normal steps:

    1. Set a break point at this line.
    2. Run the program, inspect the value of a and b.
    3. Figure out whether the error lies in a or b.
    4. Set another break point, repeat 🔁

    Or, if you want to do it in one run:

    1. Set a break point at the entry point of the program.
    2. Step through and program and remember everything happened along the way.
    3. Stop at c = a + b, use your brain to infer what happened.

    Either way, we still need to spend time reading the code and following the execution. We also need to keep monitoring all relevant variables in every step, compare them with the expected values, and memorize the results, because debuggers don't persist them. This is a huge overhead to our brain, and as a result made debugging less efficient and sometimes frustrating.

    The problem is obvious: debuggers only give clues, without telling why. We've been taking the manual work for granted for so long, that we don't even think it's a problem. In fact it is a problem, and it can be solved.

Part 4: A novel approach to tackle the pain points

To reiterate, An ideal debugging tool should

  • Easy-to-use and provide rich information.
  • Tell you why a variable has a wrong value with no or minimal human intervention.

For a moment, let's forget about the tools we use every day, just think about one question: who has the information we need for debugging?

The answer is: the Python interpreter.

So the question becomes, how do we pull relevant information out of the interpreter?

I will briefly introduce the sys.settrace API, and the opcode event introduced in Python 3.7, with the example of c = a + b to demonstrate using bytecode to trace the sources of a variable. In this case, the sources of c are a and b. With this power, reasoning the root cause of a wrong value becomes possible.

I will then introduce Cyberbrain, a debugger that takes advantage of the power of bytecode to solve the pain points with variable backtracing. What it means is that, besides viewing each variable's value at every step (which Cyberbrain also supports), users can "see" the sources of each variable change in a visualized way. In the previous example, Cyberbrain will tell you that it's a and b that caused c to change. It also traces the sources of a and b all the way up to the point where tracing begins.

I'll do a quick demo of using Cyberbrain to debug a program to show how it solves the two pain points. By the end of the demo, the audience will realize that traditional debugging tools do require a lot of manual effort which can be automated.

Bytecode tracing also has its problems, like it can make program slower and generate a huge amount of data for long running programs. But the important thing is that we realize the pain points, and don't stop looking for new possibilities, which brings the next topic.

Part 5: Where do we go from here?

Now is an interesting time.

On the one hand, existing tools are becoming calcified. Debug Adapter Protocol is gaining popularity, which defines the capabilities a debugger should provide. Tools that conform to DAP will never be able to provide capabilities beyond what the protocol specifies.

On the other hand, new tools are coming out in Python's debugging space, just to list a few:

  • PySnooper, IceCream, Hunter, pytrace: lets you trace function calls and variables with no effort, automating the process of adding print().
  • birdseye, Thonny: graphical debuggers that can visualize the values of expressions.
  • Python Tutor: web-based interactive program visualization, which also visualizes data structures.
  • Cyberbrain.

These new tools share the same goal of reducing programmers' work in debugging, but beyond that, they are both trying to pitch the idea to people that the current "standard" way of debugging is not good enough, that more things can be achieved with less manual effort. The ideas behind are even more important than the tools themselves.

Why is this important? Dijkstra has some famous words:

The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities.

Imagine a world where all these efforts don't exist, will the word "debugger" gradually change from "something that can help you debug" to "something that conforms to the Debug Adapter Protocol"? That is not impossible. We need to prevent it from becoming the truth, and preserve a possible future where programmers are debugging in an effortless and more efficient way. So what can we do?

  • Think of new ways to make debugging better;
  • Create tools, or contribute to them;
  • Spread this talk and the ideas;
  • Create new programming languages that put debuggability as the core feature.

And the easiest, yet hardest thing: keep an open mind.

Why Is GIL Worse Than We Thought?

以前每当看到有人抱怨 GIL(Global Interpreter Lock),我总会告诉他们不用慌,各种场景都有对应的解决方案,比如主 IO 操作用 async,主 CPU 操作用多进程。我也一直认为,Python 的慢主要慢在“纯”执行速度,而 GIL 只不过是一个瑕疵。

然而最近我意识到,GIL 是一个比想象中严重得多的问题,因为它阻碍了程序的按需并行

什么是“按需并行”?这个词是我造的,用来描述编程中的一种常见 pattern,即把最耗时的那部分操作并行化,而程序整体仍保持单线程。通常来讲,耗时的部分往往是在遍历一个巨大的列表,并对列表中的元素做某种操作。而并行化也非常简单,只要开多个线程分别处理列表的一部分就行了。

这里我们只讨论 CPU 密集的情况。由于 GIL 的存在,开多个线程并不会让程序跑得更快(如果不是更慢的话),因此我们必须用到多进程。那么多进程是不是就能解决问题呢?并不总是,有一系列难点:

  • 进程不共享内存,计算的输入必须被传到每个工作进程里,比如列表中的元素;
  • 能被传递的东西必须 picklable,而有相当多的东西是 unpicklable 的;
  • 如果后续程序执行需要并行计算的输出,那么这些输出也得 picklable;
  • Pickle -> unpickle 操作带来了额外的性能开销。

这样一来,多进程的应用范围就大大减小了。比如我最近在 Cyberbrain 中遇到的一个问题,其中一段代码是这样的:

for event in frame.events:   
  frame_proto.events.append(_transform_event_to_proto(event))
    event_ids = _get_event_sources_uids(event, frame)
    if event_ids:
      frame_proto.tracing_result[event.uid].event_ids[:] = event_ids

这段代码遍历 frame.events,处理之后更新 frame_protoevents 数量很大,导致这部分代码成为了性能瓶颈,因此我想把它并行化。然后我就发现这是一个不可能完成的任务,为什么呢?因为 protocol buffer 对象不 pickable。这意味着,我既不能把 frame_proto 传进每个进程,也不能把 _transform_event_to_proto(event) 的结果传出来,因为它们都是 protocol buffer 对象。如果是 C++ 或者 Java,这里直接多线程就解决了(每个线程分别更新 frame_proto)。

总结一下:

  • GIL 让在大部分语言里可以用多线程解决的事必须要用多进程解决。
  • 多进程的诸多限制让它无法无缝替代多线程。即使在能替代的场景,也要做很多额外工作,以及承担序列化和反序列化带来的性能开销。

之前我们探讨了 GIL 对“并行”的阻碍,下面聊聊 GIL 对“按需”的阻碍。这是更本质的问题,却极少被人注意到。我们都知道,过早的优化是万恶之源。除了明显需要优化的场景(比如避免数据库 N+1),一般而言都是先实现,再 profile,最后优化。换句话说,类似“一个循环成了性能瓶颈”这种发现,写代码的时候一般是不知道的。假设你的程序写完了,然后需要优化某一部分,你当然希望能够不动其它代码,只修改瓶颈部分即可。这种优化的场景就非常适合多线程——因为变量是共享的,所以程序的整体完全不用动。而一旦涉及多进程,则往往需要对程序进行更大程度的修改,甚至重新设计整个架构。这样一来,“按需”优化就不存在了。这不仅导致优化困难,更给项目管理带来了不确定性,甚至可能导致延期或性能不达标。

那么,PEP 554 - 多解释器 是不是救世主呢?显然也不是。多解释器说白了就是 goroutine 的 Python 实现,问题是它限制了 channel 能传递的变量类型,quote:

Along those same lines, we will initially restrict the types that may be passed through channels to the following:

  • None
  • bytes
  • str
  • int
  • channels

所以,多解释器虽然是好的,但恐怕还是不能解决“按需并行”的问题。


注:Python 里多进程可以共享内存,然而能共享的变量类型同样有限,具体可参考:multiprocessing.shared_memory

Update: 发现一个遇到了类似问题的哥们儿,以及我的回复


top