8000 threading module is missing basic/introductory usage example · Issue #124210 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

threading module is missing basic/introductory usage example #124210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matkoniecz opened this issue Sep 18, 2024 · 11 comments
Closed

threading module is missing basic/introductory usage example #124210

matkoniecz opened this issue Sep 18, 2024 · 11 comments
Labels
docs Documentation in the Doc dir easy

Comments

@matkoniecz
Copy link
matkoniecz commented Sep 18, 2024

Documentation

threading module is missing basic/introductory usage example

documentation start from some general notes which may have its place there and jump into "This module defines the following functions: threading.active_count()"

Would it be maybe possible to have an example of basic standard usage? For "my Python script is CPU-limited, I want to use more than one core"

the closest that I found is https://docs.python.org/3/library/threading.html#threading.Thread.run that does not really explain at all how it is actually supposed to be used even for a toy example

(I will probably find tutorial somewhere, maybe chatgpt/deepseek will produce something but I would love to have an official example that I can assume to be a good idea rather than finding something that seems to work)

Linked PRs

@matkoniecz matkoniecz added the docs Documentation in the Doc dir label Sep 18, 2024
@matkoniecz
Copy link
Author
matkoniecz commented Sep 18, 2024

I am not claiming that it is a good code (I was in fact looking in docs as I have no clear idea how ideal/typical code is expected to look like) but following seems to work and sort-of-demonstrates threading (admittedly, this one does not demonstrate that GIL was worked around but I am going to trust docs that it will work also for CPU-heavy things):

import threading
import time

class SingleScan():
    def __init__(self, link):
        self.link = link

    def crawl(self):
        print("CRAWL STARTED for", self.link)
        time.sleep(3)
        print("CRAWL ENDED for", self.link)

s = SingleScan("https://example.com")
t1 = threading.Thread(target = s.crawl)
t1.start()

s = SingleScan("http://example.com")
t2 = threading.Thread(target = s.crawl)
t2.start()

(yes, code above is likely terrible, that is why I tried checking official docs first)

BTW, I was initially confused as I had s.crawl() and it was sequential but without obvious failures :)

@gaogaotiantian
Copy link
Member

my Python script is CPU-limited, I want to use more than one core

You can't :(. At least for now.

If you read the docs, it's actually mentioned in the very early section in "CPython implementation detail".

I'm not a docs expert, but I think people have different opinions of docs. I'm not against examples in docs but I think most of the time Python docs gives examples immediately after the documented methods. What you are looking for might be a "tutorial" about how to use threading module - which is something that Python tries to cover for many aspects (https://docs.python.org/3/howto/index.html).

@matkoniecz
Copy link
Author

oh right, in such case https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing should be used which actually has a basic example

still, it would be nice for threading to have something comparable

@ZeroIntensity
Copy link
Member

Would you like to submit a PR for that?

@matkoniecz
Copy link
Author

Sadly, I am not able as I have no idea what is the proper way to code this. Unless something like #124210 (comment) is actually a good example.

@ZeroIntensity
Copy link
Member

It's a working example, but maybe we want something that has args and kwargs with it.

@donBarbos
Copy link
Contributor

what about next example:

import threading
import time

def crawl(link, delay=3):
    print(f"crawl started for {link}")
    time.sleep(delay)
    print(f"crawl ended for {link}")

links = [
    "https://example.com",
    "https://another-example.com",
    "https://yet-another-example.com"
]

# Start threads for each link
threads = []
for link in links:
    # Using `args` to pass positional arguments and `kwargs` for keyword arguments
    t = threading.Thread(target=crawl, args=(link,), kwargs={"delay": 2})
    threads.append(t)
    t.start()

# Wait for all threads to finish
for t in threads:
    t.join()

@ZeroIntensity
Copy link
Member

I like it :)

@hridyasadanand
Copy link

Hi! I’m Hridya, a first-year BTech student learning Python. I’d love to work on this issue as part of my GSoC preparation. Can I take it?

@ZeroIntensity
Copy link
Member

There's already a PR up, but reviewing PRs is a great way to contribute as well.

hugovk pushed a commit that referenced this issue May 16, 2025
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 16, 2025
(cherry picked from commit 62f66ca)

Co-authored-by: Semyon Moroz <donbarbos@proton.me>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 16, 2025
(cherry picked from commit 62f66ca)

Co-authored-by: Semyon Moroz <donbarbos@proton.me>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
hugovk pushed a commit that referenced this issue May 16, 2025
…134091)

Co-authored-by: Semyon Moroz <donbarbos@proton.me>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
hugovk pushed a commit that referenced this issue May 16, 2025
…134090)

Co-authored-by: Semyon Moroz <donbarbos@proton.me>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
@donBarbos
Copy link
Contributor
donBarbos commented May 16, 2025

@hugovk I think we can close this issue as completed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir easy
Projects
None yet
Development

No branches or pull requests

5 participants
0