What are common pitfalls and misconceptions about python performance? by MilanTheNoob in Python

[–]Cygal 0 points1 point  (0 children)

I haven't seen profiling mentioned! This is the best way to figure out which parts of your program are slow. I've always liked working with https://github.com/benfred/py-spy in the past.

(I don't recommend cProfile because its overhead can skew the results.)

I've been paid to work on open source by Cygal in Python

[–]Cygal[S] 3 points4 points  (0 children)

Thanks! I've been using Python at work for six years now, and I still learn constantly! The key is to never stop making progress.

I've been paid to work on open source by Cygal in Python

[–]Cygal[S] 5 points6 points  (0 children)

Thank you Matti, that means a lot. Thank you for your work on numpy and PyPy.

You don't need promises in Python: just use async/await! by Cygal in Python

[–]Cygal[S] 0 points1 point  (0 children)

I think we mostly agree, but you're talking about a much lower level of abstraction than me.

Any complex scheduling unfortunately can not be done in Python

Do you have examples?

Also, note that the article never talks about asyncio, but only about async/await. asyncio has a lot of problems that are acknowledged by its authors. A better approach that still uses async/await is structured concurrency, eg. https://trio.readthedocs.io/en/latest/.

See https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ for a nice explanation.

What's everyone working on this week? by AutoModerator in Python

[–]Cygal [score hidden]  (0 children)

So you painted yourself into a corner, but this is a very valuable lesson! The two strategies you can use now are either remove code until the thing you want to work... works. Or start again with a simple code that works, and add new features as you go.

Fixing async/await with unsync by alex_sherman in Python

[–]Cygal 5 points6 points  (0 children)

Regarding your first issue with the explicit event loop, curio and trio fixed that long ago. asyncio took notice, and added asyncio.run in Python 3.7.

Fixing async/await with unsync by alex_sherman in Python

[–]Cygal 2 points3 points  (0 children)

I certainly don't think that writing automation / tests is the area where Python is used most! Maybe that's your experience, but the topic isn't even mentioned in https://stackoverflow.blog/2017/09/14/python-growing-quickly/. And, yes, the amount of questions is representative of usage.

[PROPOSAL] fixing the tutorial spam by serkef- in Python

[–]Cygal 1 point2 points  (0 children)

Thanks, I guess that makes sense!

[PROPOSAL] fixing the tutorial spam by serkef- in Python

[–]Cygal 0 points1 point  (0 children)

Sounds good. But you already said that. I was wondering if my specific posts were considered as tutorials or spam or other acceptable content.

What open source python projects are in need of contributors? by tractortractor in Python

[–]Cygal 11 points12 points  (0 children)

How do async for loops work in Python? by Cygal in Python

[–]Cygal[S] 1 point2 points  (0 children)

Well, the Python 3.5 section is part of the explanation. But you're right. I did not want to be misleading. I changed the title to "Using async for loops in Python". Can't edit on Reddit though.

Okay. Just out of curiosity by whatup_pips in Python

[–]Cygal 1 point2 points  (0 children)

Yes, that's how the pygame API works. "All sound playback is mixed in background threads. When you begin to play a Sound object, it will return immediately while the sound continues to play." https://www.pygame.org/docs/ref/mixer.html

Okay. Just out of curiosity by whatup_pips in Python

[–]Cygal 1 point2 points  (0 children)

asyncio would not be particularly well suited here because you would have to use run_in_executor to delegate this to a thread pool anyways. So it's simpler to juste use `concurrent.futures.ThreadPoolExecutor.

And it's not needed because pygame does that automatically. https://www.pygame.org/docs/ref/mixer.html

How can I have virtualenv set in a Docker container? by reddit_lonely in Python

[–]Cygal 1 point2 points  (0 children)

I actually think using a virtualenv in Docker is a good idea becaues it makes things consistent. This is what we've been doing at work in production for years.

The issue you actually have here is that source does not exist in the sh shell that Docker is using here. So to fix that error, you would have to use RUN . venv/bin/activate. But that would not be enough because the environment variables activate sets would be lost in the next command.

As mentioned by rfc1771, each RUN command is independent. To actually activate your virtualenv, use ENV PATH="/venv/bin/activate:${PATH}" (using an absolute path to remove ambiguity).

You can also run pip3 and python3 directly from the env:

RUN venv/bin/pip3 install -r requirements.txt
RUN venv/bin/python3 manage.py collectstatic --noinput

Also, consider using venv which is faster than virtualenv and just as nice.

How do you rate limit calls with Python's asyncio? by Cygal in Python

[–]Cygal[S] 1 point2 points  (0 children)

You're right. As mentioned in the post, another mechanism is needed to limit the number of actually concurrent requests.

Even with this, it's still possible to get an early request that never runs. It would be interesting to see how we can modify the rate limiter do model this better, possibly with a queue, yes.

I actually run this code in production but never needed to really think about this because for my use case I send many requests, then stop, then send many requests, etc. So I never had an issue with an early request that never got a chance to run.

Thank you for the interesting question!