NLinker

Example of pyo3-async and tokio

Mar 7th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 30.00 KB | Source Code | 0 0
  1. ./0000775000175000017500000000000014572424635007677 5ustar  vikavika./.idea/0000775000175000017500000000000014572423611010650 5ustar  vikavika./.idea/vcs.xml0000664000175000017500000000024714572410634012171 0ustar  vikavika<?xml version="1.0" encoding="UTF-8"?>
  2. <project version="4">
  3.   <component name="VcsDirectoryMappings">
  4.     <mapping directory="" vcs="Git" />
  5.   </component>
  6. </project>./.idea/modules.xml0000664000175000017500000000043414572410634013044 0ustar  vikavika<?xml version="1.0" encoding="UTF-8"?>
  7. <project version="4">
  8.   <component name="ProjectModuleManager">
  9.     <modules>
  10.       <module fileurl="file://$PROJECT_DIR$/.idea/pyo3_async_hello.iml" filepath="$PROJECT_DIR$/.idea/pyo3_async_hello.iml" />
  11.     </modules>
  12.   </component>
  13. </project>./.idea/pyo3_async_hello.iml0000664000175000017500000000065314572410727014635 0ustar  vikavika<?xml version="1.0" encoding="UTF-8"?>
  14. <module type="EMPTY_MODULE" version="4">
  15.   <component name="NewModuleRootManager">
  16.     <content url="file://$MODULE_DIR$">
  17.       <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
  18.       <excludeFolder url="file://$MODULE_DIR$/target" />
  19.     </content>
  20.     <orderEntry type="inheritedJdk" />
  21.     <orderEntry type="sourceFolder" forTests="false" />
  22.   </component>
  23. </module>./.idea/workspace.xml0000664000175000017500000001051414572423611013371 0ustar  vikavika<?xml version="1.0" encoding="UTF-8"?>
  24. <project version="4">
  25.   <component name="AutoImportSettings">
  26.     <option name="autoReloadType" value="ALL" />
  27.   </component>
  28.   <component name="CargoProjects">
  29.     <cargoProject FILE="$PROJECT_DIR$/Cargo.toml" />
  30.   </component>
  31.   <component name="ChangeListManager">
  32.     <list default="true" id="d40901e3-c1db-4149-aade-0e386a13ec15" name="Changes" comment="">
  33.       <change beforePath="$PROJECT_DIR$/Cargo.lock" beforeDir="false" afterPath="$PROJECT_DIR$/Cargo.lock" afterDir="false" />
  34.       <change beforePath="$PROJECT_DIR$/Cargo.toml" beforeDir="false" afterPath="$PROJECT_DIR$/Cargo.toml" afterDir="false" />
  35.       <change beforePath="$PROJECT_DIR$/src/lib.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/lib.rs" afterDir="false" />
  36.     </list>
  37.     <option name="SHOW_DIALOG" value="false" />
  38.     <option name="HIGHLIGHT_CONFLICTS" value="true" />
  39.     <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
  40.     <option name="LAST_RESOLUTION" value="IGNORE" />
  41.   </component>
  42.   <component name="Git.Settings">
  43.     <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
  44.   </component>
  45.   <component name="MacroExpansionManager">
  46.     <option name="directoryName" value="8rk9tufw" />
  47.   </component>
  48.   <component name="MarkdownSettingsMigration">
  49.     <option name="stateVersion" value="1" />
  50.   </component>
  51.   <component name="ProjectColorInfo"><![CDATA[{
  52.   "associatedIndex": 1
  53. }]]></component>
  54.   <component name="ProjectId" id="2dNBMuNWXeOw43y579KeVOIVTC7" />
  55.   <component name="ProjectViewState">
  56.     <option name="hideEmptyMiddlePackages" value="true" />
  57.     <option name="showLibraryContents" value="true" />
  58.   </component>
  59.   <component name="PropertiesComponent"><![CDATA[{
  60.   "keyToString": {
  61.     "Cargo.build.executor": "Run",
  62.     "RunOnceActivity.OpenProjectViewOnStart": "true",
  63.     "RunOnceActivity.ShowReadmeOnStart": "true",
  64.     "RunOnceActivity.rust.reset.selective.auto.import": "true",
  65.     "git-widget-placeholder": "main",
  66.     "last_opened_file_path": "/home/vika/rust/pyo3_async_hello",
  67.     "node.js.detected.package.eslint": "true",
  68.     "node.js.detected.package.tslint": "true",
  69.     "node.js.selected.package.eslint": "(autodetect)",
  70.     "node.js.selected.package.tslint": "(autodetect)",
  71.     "nodejs_package_manager_path": "npm",
  72.     "org.rust.cargo.project.model.PROJECT_DISCOVERY": "true",
  73.     "org.rust.cargo.project.model.impl.CargoExternalSystemProjectAware.subscribe.first.balloon": "",
  74.     "vue.rearranger.settings.migration": "true"
  75.   }
  76. }]]></component>
  77.   <component name="RunManager">
  78.     <configuration name="build" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
  79.       <option name="command" value="build" />
  80.       <option name="workingDirectory" value="file://$PROJECT_DIR$" />
  81.       <envs />
  82.       <option name="emulateTerminal" value="true" />
  83.       <option name="channel" value="DEFAULT" />
  84.       <option name="requiredFeatures" value="true" />
  85.       <option name="allFeatures" value="false" />
  86.       <option name="withSudo" value="false" />
  87.       <option name="buildTarget" value="REMOTE" />
  88.       <option name="backtrace" value="SHORT" />
  89.       <option name="isRedirectInput" value="false" />
  90.       <option name="redirectInputPath" value="" />
  91.       <method v="2">
  92.         <option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
  93.       </method>
  94.     </configuration>
  95.     <recent_temporary>
  96.       <list>
  97.         <item itemvalue="Cargo.build" />
  98.       </list>
  99.     </recent_temporary>
  100.   </component>
  101.   <component name="RustProjectSettings">
  102.     <option name="toolchainHomeDirectory" value="$USER_HOME$/.cargo/bin" />
  103.   </component>
  104.   <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
  105.   <component name="TaskManager">
  106.     <task active="true" id="Default" summary="Default task">
  107.       <changelist id="d40901e3-c1db-4149-aade-0e386a13ec15" name="Changes" comment="" />
  108.       <created>1709838611384</created>
  109.       <option name="number" value="Default" />
  110.       <option name="presentableId" value="Default" />
  111.       <updated>1709838611384</updated>
  112.       <workItem from="1709838616432" duration="5229000" />
  113.     </task>
  114.     <servers />
  115.   </component>
  116.   <component name="TypeScriptGeneratedFilesManager">
  117.     <option name="version" value="3" />
  118.   </component>
  119. </project>./.idea/.gitignore0000664000175000017500000000026014572410435012636 0ustar  vikavika# Default ignored files
  120. /shelf/
  121. /workspace.xml
  122. # Editor-based HTTP Client requests
  123. /httpRequests/
  124. # Datasource local storage ignored files
  125. /dataSources/
  126. /dataSources.local.xml
  127. ./Cargo.toml0000664000175000017500000000065214572412775011634 0ustar  vikavika[package]
  128. name = "pyo3_async_hello"
  129. version = "0.1.0"
  130. edition = "2021"
  131.  
  132. # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
  133. [lib]
  134. name = "pyo3_async_hello"
  135. crate-type = ["cdylib"]
  136.  
  137. [dependencies]
  138. pyo3 = "0.20.3"
  139. pyo3-asyncio = { version = "0.20.0", features = ["attributes", "tokio-runtime", "pyo3-asyncio-macros"] }
  140. tokio = { version = "1", features = ["time"] }
  141. reqwest = "0.11"
  142. ./README.md0000664000175000017500000000122714572423603011152 0ustar  vikavika# pyo3 async example
  143.  
  144. ## prepare system
  145.  
  146. ```shell
  147. sudo add-apt-repository ppa:deadsnakes/ppa -y
  148. sudo apt update
  149. sudo apt install python3.12 python3.12-{dev,dbg,venv,gdbm} -y
  150.  
  151. # to build tokio without rustls feature enabled
  152. sudo apt install libssl-dev
  153.  
  154. # this installed to ~/.local/lib/python3.8/site-packages/pipx
  155. python3 -m pip install --user pipx
  156. python3 -m pipx ensurepath
  157.  
  158. # this installed to ~/.local/share/pipx/venvs/maturin
  159. pipx install maturin
  160. ```
  161.  
  162. ## prepare project
  163.  
  164. ```shell
  165. python3.12 -m venv .venv
  166. source .venv/bin/activate
  167. ```
  168.  
  169. ## prepare build
  170.  
  171. ```shell
  172. maturin develop
  173. ```
  174. To distribute we need to build wheel, which is built with `maturin build`../pyproject.toml0000664000175000017500000000057014572403237012610 0ustar  vikavika[build-system]
  175. requires = ["maturin>=1.2,<2.0"]
  176. build-backend = "maturin"
  177.  
  178. [project]
  179. name = "pyo3_async_hello"
  180. requires-python = ">=3.7"
  181. classifiers = [
  182.     "Programming Language :: Rust",
  183.     "Programming Language :: Python :: Implementation :: CPython",
  184.     "Programming Language :: Python :: Implementation :: PyPy",
  185. ]
  186.  
  187.  
  188. [tool.maturin]
  189. features = ["pyo3/extension-module"]
  190. ./.github/0000775000175000017500000000000014572403237011232 5ustar  vikavika./.github/workflows/0000775000175000017500000000000014572403237013267 5ustar  vikavika./.github/workflows/CI.yml0000664000175000017500000000541114572403237014306 0ustar  vikavika# This file is autogenerated by maturin v1.2.3
  191. # To update, run
  192. #
  193. #    maturin generate-ci github
  194. #
  195. name: CI
  196.  
  197. on:
  198.   push:
  199.     branches:
  200.       - main
  201.       - master
  202.     tags:
  203.       - '*'
  204.   pull_request:
  205.   workflow_dispatch:
  206.  
  207. permissions:
  208.   contents: read
  209.  
  210. jobs:
  211.   linux:
  212.     runs-on: ubuntu-latest
  213.     strategy:
  214.       matrix:
  215.         target: [x86_64, x86, aarch64, armv7, s390x, ppc64le]
  216.     steps:
  217.       - uses: actions/checkout@v3
  218.       - uses: actions/setup-python@v4
  219.         with:
  220.           python-version: '3.10'
  221.       - name: Build wheels
  222.         uses: PyO3/maturin-action@v1
  223.         with:
  224.           target: ${{ matrix.target }}
  225.           args: --release --out dist --find-interpreter
  226.           sccache: 'true'
  227.           manylinux: auto
  228.       - name: Upload wheels
  229.         uses: actions/upload-artifact@v3
  230.         with:
  231.           name: wheels
  232.           path: dist
  233.  
  234.   windows:
  235.     runs-on: windows-latest
  236.     strategy:
  237.       matrix:
  238.         target: [x64, x86]
  239.     steps:
  240.       - uses: actions/checkout@v3
  241.       - uses: actions/setup-python@v4
  242.         with:
  243.           python-version: '3.10'
  244.           architecture: ${{ matrix.target }}
  245.       - name: Build wheels
  246.         uses: PyO3/maturin-action@v1
  247.         with:
  248.           target: ${{ matrix.target }}
  249.           args: --release --out dist --find-interpreter
  250.           sccache: 'true'
  251.       - name: Upload wheels
  252.         uses: actions/upload-artifact@v3
  253.         with:
  254.           name: wheels
  255.           path: dist
  256.  
  257.   macos:
  258.     runs-on: macos-latest
  259.     strategy:
  260.       matrix:
  261.         target: [x86_64, aarch64]
  262.     steps:
  263.       - uses: actions/checkout@v3
  264.       - uses: actions/setup-python@v4
  265.         with:
  266.           python-version: '3.10'
  267.       - name: Build wheels
  268.         uses: PyO3/maturin-action@v1
  269.         with:
  270.           target: ${{ matrix.target }}
  271.           args: --release --out dist --find-interpreter
  272.           sccache: 'true'
  273.       - name: Upload wheels
  274.         uses: actions/upload-artifact@v3
  275.         with:
  276.           name: wheels
  277.           path: dist
  278.  
  279.   sdist:
  280.     runs-on: ubuntu-latest
  281.     steps:
  282.       - uses: actions/checkout@v3
  283.       - name: Build sdist
  284.         uses: PyO3/maturin-action@v1
  285.         with:
  286.           command: sdist
  287.           args: --out dist
  288.       - name: Upload sdist
  289.         uses: actions/upload-artifact@v3
  290.         with:
  291.           name: wheels
  292.           path: dist
  293.  
  294.   release:
  295.     name: Release
  296.     runs-on: ubuntu-latest
  297.     if: "startsWith(github.ref, 'refs/tags/')"
  298.     needs: [linux, windows, macos, sdist]
  299.     steps:
  300.       - uses: actions/download-artifact@v3
  301.         with:
  302.           name: wheels
  303.       - name: Publish to PyPI
  304.         uses: PyO3/maturin-action@v1
  305.         env:
  306.           MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
  307.         with:
  308.           command: upload
  309.           args: --non-interactive --skip-existing *
  310. ./src/0000775000175000017500000000000014572422320010453 5ustar  vikavika./src/lib.rs0000664000175000017500000000074614572422320011576 0ustar  vikavikause pyo3::prelude::*;
  311. use pyo3_asyncio::tokio::*;
  312.  
  313. #[pyfunction]
  314. fn hello(py: Python) -> PyResult<&PyAny> {
  315.     future_into_py(py, async {
  316.         println!("start waiting...");
  317.         tokio::time::sleep(std::time::Duration::from_secs(1)).await;
  318.         println!("Hello, Kate!");
  319.         Ok(())
  320.     })
  321. }
  322.  
  323. /// A Python module implemented in Rust.
  324. #[pymodule]
  325. fn pyo3_async_hello(_py: Python, m: &PyModule) -> PyResult<()> {
  326.     m.add_function(wrap_pyfunction!(hello, m)?)?;
  327.     Ok(())
  328. }
  329. ./.gitignore0000664000175000017500000000125514572403237011665 0ustar  vikavika/target
  330.  
  331. # Byte-compiled / optimized / DLL files
  332. __pycache__/
  333. .pytest_cache/
  334. *.py[cod]
  335.  
  336. # C extensions
  337. *.so
  338.  
  339. # Distribution / packaging
  340. .Python
  341. .venv/
  342. env/
  343. bin/
  344. build/
  345. develop-eggs/
  346. dist/
  347. eggs/
  348. lib/
  349. lib64/
  350. parts/
  351. sdist/
  352. var/
  353. include/
  354. man/
  355. venv/
  356. *.egg-info/
  357. .installed.cfg
  358. *.egg
  359.  
  360. # Installer logs
  361. pip-log.txt
  362. pip-delete-this-directory.txt
  363. pip-selfcheck.json
  364.  
  365. # Unit test / coverage reports
  366. htmlcov/
  367. .tox/
  368. .coverage
  369. .cache
  370. nosetests.xml
  371. coverage.xml
  372.  
  373. # Translations
  374. *.mo
  375.  
  376. # Mr Developer
  377. .mr.developer.cfg
  378. .project
  379. .pydevproject
  380.  
  381. # Rope
  382. .ropeproject
  383.  
  384. # Django stuff:
  385. *.log
  386. *.pot
  387.  
  388. .DS_Store
  389.  
  390. # Sphinx documentation
  391. docs/_build/
  392.  
  393. # PyCharm
  394. .idea/
  395.  
  396. # VSCode
  397. .vscode/
  398.  
  399. # Pyenv
  400. .python-version
Add Comment
Please, Sign In to add comment