CodeSampleX

示例

greenlet 3.2.4: coroutine task switching with greenlet

已验证示例 — pypi greenlet 3.2.4: coroutine task switching with greenlet. contract 在 python 3.12 · linux alpine/x64 · docker 上运行并通过.

sha256:18a8ff77421e3e57c240e216e3bbb1aa8726f9b2077e36c5e46f023d16a27a1e

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 python linux 24 · ubuntu · glibc 2.39 x64 python python pip

验证运行环境

环境 契约 阶段 运行日期
python 3.12 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-19

案例

HOW
目标
coroutine task switching with greenlet
包
符号
  • greenlet.greenlet
环境
python
创建时间
2026-08-19T18:52:48Z

契约

  1. greenlet coroutines switch execution back and forth passing values and tracking lifecycle state

文件

  • PROMPT.md
  • csx.json
  • requirements.txt
  • sample.py
  • spec.json
  • test/contract.py

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: coroutine task switching with greenlet
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:pypi/greenlet@3.2.4
Demonstrate these symbols/APIs:
  - greenlet.greenlet
Required runtime conditions:
  - ecosystem: pypi
  - language: python
  - packageManager: pip
  - runtime: python

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:f941671c65edbc00f8712d84e791b5651faceeb36498b4b609ebff85a08278e9","contract":["greenlet coroutines switch execution back and forth passing values and tracking lifecycle state"],"goal":"coroutine task switching with greenlet","kind":"HOW","packages":["pkg:pypi/greenlet@3.2.4"],"schemaVersion":1,"symbols":["greenlet.greenlet"]},"contractCommand":["python","test/contract.py"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"pypi","language":"python","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"pip","runtime":"python","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/greenlet@3.2.4"],"schemaVersion":1,"symbols":["greenlet.greenlet"],"verifierAdapter":"python@1"}
requirements.txt
greenlet==3.2.4
sample.py
from greenlet import greenlet


def run_cooperative_tasks():
    events = []

    def task_alpha(initial_msg):
        events.append(f"alpha_started:{initial_msg}")
        beta_reply = gr_beta.switch("from_alpha_step1")
        events.append(f"alpha_resumed:{beta_reply}")
        return "alpha_finished"

    def task_beta(msg_from_alpha):
        events.append(f"beta_started:{msg_from_alpha}")
        alpha_reply = gr_alpha.switch("from_beta_step1")
        events.append(f"beta_resumed:{alpha_reply}")
        return "beta_finished"

    gr_alpha = greenlet(task_alpha)
    gr_beta = greenlet(task_beta)

    res_a = gr_alpha.switch("init")
    res_b = gr_beta.switch("continue_beta")

    return {
        "alpha_result": res_a,
        "beta_result": res_b,
        "events": events,
        "alpha_dead": gr_alpha.dead,
        "beta_dead": gr_beta.dead,
    }


if __name__ == "__main__":
    result = run_cooperative_tasks()
    print("Cooperative execution:", result)
spec.json
{
  "schemaVersion": 1,
  "goal": "coroutine task switching with greenlet",
  "kind": "HOW",
  "packages": [
    "pkg:pypi/greenlet@3.2.4"
  ],
  "symbols": [
    "greenlet.greenlet"
  ],
  "runtimeConditions": {
    "ecosystem": "pypi",
    "language": "python",
    "packageManager": "pip",
    "runtime": "python"
  }
}
test/contract.py
import sys
from greenlet import greenlet


def test_greenlet_switching():
    events = []

    def coroutine_a(val):
        events.append(f"a_start:{val}")
        resp = g_b.switch("msg_from_a")
        events.append(f"a_resume:{resp}")
        return "result_a"

    def coroutine_b(val):
        events.append(f"b_start:{val}")
        resp = g_a.switch("msg_from_b")
        events.append(f"b_resume:{resp}")
        return "result_b"

    g_a = greenlet(coroutine_a)
    g_b = greenlet(coroutine_b)

    assert not g_a.dead
    assert not g_b.dead

    res_a = g_a.switch("init_a")
    assert res_a == "result_a"
    assert g_a.dead
    assert not g_b.dead
    assert events == ["a_start:init_a", "b_start:msg_from_a", "a_resume:msg_from_b"]

    res_b = g_b.switch("resume_b")
    assert res_b == "result_b"
    assert g_b.dead
    assert events == [
        "a_start:init_a",
        "b_start:msg_from_a",
        "a_resume:msg_from_b",
        "b_resume:resume_b",
    ]


def main():
    test_greenlet_switching()
    print("Contract verification passed.")


if __name__ == "__main__":
    main()

原始种子者

匿名