teleren/devto huggingface dataset:
The perils of get_or_create: race conditions
Tags: python, django, orm
Published: 2020-01-19 03:09:34

---
title: The perils of get_or_create: race conditions
published: true
date: 2018-02-02 18:34:00 UTC
tags: python, django, ORM
canonical_url: https://adriennedomingus.com/blog/the-perils-of-getorcreate-race-conditions
---

`get_or_create`, is an awesome helper utility, until it’s not. There is a nod to what we’re about to talk about in the [docs](https://docs.djangoproject.com/en/2.0/ref/models/querysets/#get-or-create):

> _This method is atomic assuming correct usage, correct database configuration, and correct behavior of the underlying database. However, if uniqueness is not enforced at the database level for the _`kwargs`_ used in a _`get_or_create`_ call (see _`unique`_ or_`unique_together`_), this method is prone to a race-condition which can result in multiple rows with the same parameters being inserted simultaneously._

Lets talk about it in more detail. Here’s the interesting bits of the source code for `get_or_create`:

```
lookup, params = self._extract_model_params(defaults, **kwargs)
try:
    return self.get(**lookup), False
except self.model.DoesNotExist:
    return self._create_object_from_params(lookup, params)
```

Pretty self-explanatory, and does exactly what the name implies. And if you go further into `_create_object_from_params`, you’ll notice that it does a lot more than just make a call to `.create()`. Here’s what happens there:

```
try:
    with transaction.atomic(using=self.db):
        obj = self.create(**params)
    return obj, True
except IntegrityError:
    exc_info = sys.exc_info()
    try:
        return self.get(**lookup), False
    except self.model.DoesNotExist:
        pass
    six.reraise(*exc_info)
```

This is cool — it’s actually accounting for race conditions. It tries to create the object, but if that operation throws an `IntegrityError`, it does the lookup again and tries to return what it finds.

The problem is this: if you hit this part of the code in one thread (meaning the lookup has already taken place and not returned anything) _on an object that does not have a uniqueness constraint on the attributes you’re doing the lookup based on_, if one is created in another thread, the creation in this thread will  **not**  throw an `IntegrityError`, and you’ll end up with two! This may be fine — for now. After all, your call to `get_or_create` returned an instance matching your parameters, and so did the call in the other thread, and both will carry on their merry way.

The problem arises _next time_ you try to retrieve the object using the same lookup params with `get_or_create`. Because you now have two objects in your database, when `get_or_create` tries its `.get()`, (note, not `.filter()`), you’ll get a `MultipleObjectsReturned` error, and no way out unless you catch this exception yourself.

The moral of the story? Don’t use `get_or_create` on objects that don’t have uniqueness constraints on the attributes you’re doing the lookup based on,_at the database level_.
dev.to Monthly Report -- January 2018
Tags: meta
Published: 2018-02-02 17:55:27

---
title: dev.to Monthly Report -- January 2018
published: true
description: Our monthly report to the dev.to community ❤️
tags: meta
cover_image: https://thepracticaldev.s3.amazonaws.com/i/fn26afgw5i3hjbmnhawv.png
---

Happy February, everyone!! 

January was busy to say the least. Let's go over some of the highlights: 

**Sustaining Members**: We launched a _pay-what-you-can_ model for [ memberships last week](https://dev.to/membership), and we already have over 100 sustaining members! We're so appreciative of the early support and are working hard to provide more perks and interesting goodies for you all. 

**Workshop Pass**: Every sustaining member who contributes $25+/month will have access to [DEV workshops](https://dev.to/workshops)! This is a new initiative for us and we're excited to kick off February with some awesome speakers (looking at you @aspittel, @cassidoo, @annarankin -- oh, and @ben)

**Scholarships:** We don't want anyone to feel excluded from opportunities, so we've created a scholarship fund for dev.to members that are interested in DEV workshops but don't have the means to be a sustaining member. We encourage anyone interested to [apply](https://dev.to/p/scholarships).

**Ask Me Anything!:** Another highlight this month was an [AMA](https://dev.to/sandimetz/im-sandi-metz-ask-me-anything-4ff9) with @sandimetz -- as a Ruby shop ourselves, it was particularly exciting to have her be our guest this month. If there's anyone specific you'd like to hear from, [let me know](https://dev.to/jess/who-should-our-next-ask-me-anything-guest-be-on-devto-4m7g).  

**New Team Member:** We welcomed a new team member on board, Liana! She's an expert at juggling a million things at once and we're so excited to have her on the team. She's already organizing some awesome workshops and turning photography & video production into a real strength of ours.

**Features:** We introduced a several new things this month, including the oft-requested reading list. It is already a nice area to manage and sort any posts you might have found interesting but did not have the urge or time to read it at the moment. This is a space we plan to improve on in the future as a way to manage the content most interesting to you.

**Some Numbers:** We are up to 56,000 registered devs and had a little over 1.1 million sessions in January. About 2,000 total posts were added to folks' reading lists this month since we launched the feature. It's nice to see some initial use of this and we're excited to see how we can make it more useful.

As always, we're here to answer any questions on the above updates, or anything else. Thanks for another great month!
Prometheus, Python, Flask
Tags: python, kubernetes, gunicorn, flask
Published: 2018-03-03 08:51:51

---
title: "Prometheus, Python, Flask"
published: true
tags: python,kubernetes,gunicorn,flask
canonical_url: 
---

 **Python** based apps are significant part of the ecosystem in our company. In the era of microservices there is really no difference which technology is in use under the hood. But each service should provide its own API and the way to monitor its&nbsp;state.

In our setup we use [**Prometheus**](https://prometheus.io/) to aggregate metrics from all our services. And it works fine. Especially with our **JVM** apps. But that was not a case for all out Pythons till&nbsp;today.

As a historical tradition our Pythons apps are mostly done with [**Flask**](http://flask.pocoo.org/). And it is a nice framework. In some cases it is also wrapped with [**Gunicorn**](http://gunicorn.org/). And here is a tutorial how to make it work with Prometheus:

1. Add the dependency to your project **setup.py** to prometheus\_client==0.1.1
2. Inside your application define some metrics you want to collect, for example: FLASK\_REQUEST\_LATENCY = Histogram(\_\_name\_\_.replace(‘.’, ‘\_’) + ‘\_request\_latency\_seconds’, ‘Flask Request Latency’)&nbsp;.
3. Annotate a method you want to measure with @FLASK\_REQUEST\_LATENCY.time()
4. Add endpoint to expose the statistics:

{% gist https://gist.github.com/gimlet2/f4b50620f3d1b91fa894636a0b72336c %}

And that’s it! If your app is just plain Flask without Gunicorn. To make it work in multiprocessor scenario you need to make several additional steps:

5. Create config for Gunicorn:

{% gist https://gist.github.com/gimlet2/5b3f69375427ac54b8694a1aed9438f1 %}

6. This config can be provided to **gunicorn** cli with **-c** &nbsp;flag.

7. Add Environment variable prometheus\_multiproc\_dir that should point to a directory where Prometheus can temporarily store the metrics. If you use **Kubernetes** update the deployment descriptor to mount **Volume** with **emptyDir** &nbsp;type.

8. And the last thing to do — update code for your /stats endpoint like&nbsp;this:

{% gist https://gist.github.com/gimlet2/f52f092f479a96784e498a7e63e343df %}

I hope this short guide can be useful and will help you to make your apps measurable and system more reliable.
Friday Blast #26
Tags: fridayblast, links, post
Published: 2019-01-20 15:22:39

---
title: Friday Blast #26
published: true
tags: friday_blast,links,post
canonical_url: https://horia141.com/friday-blast-26.html
series: "Friday Blast"
---

[Understanding the C++ memory model (2018)](https://davmac.wordpress.com/2018/01/28/understanding-the-c-c-memory-model/) - a brief discussion on atomic operations and memory barriers and what they mean in the context of C/C++ (and many other languages at the same level of abstraction - Java, C#, Rust etc). Atomics are used to guarantee that certain very basic operations happen as a unit. Things like incremeting, compare-and-swap, compare-and-test etc. The set of atomic operations for a particular object forms a total order. However, this implies nothing for other objects or for visibility of other operations. For that, memory barriers are needed. C/C++ have the notion of acquire and release barriers, which guarantee that all operations before, respectively after, won’t be reordered past the barrier. Barriers say nothing about atomicity. So you need both as a basis for concurrency primitives.

[Software complexity is killing us (2018)](https://www.simplethread.com/software-complexity-killing-us/) - the main argument is really that businesses are really complex these days. There’s a lot of software which needs to do things and then a lot of interconnections between those pieces of software. Think things like - AD needs to know about new people joining the company from the HR system. OSS has been really good at providing the building blocks for software, so we don’t need to reimplement the OS/core libraries/UI frameworks/data storage etc. But higher than that there’s not much to work with. And that’s where a lot of the complexity in a business setting comes from.

[Terraforming 1Password (2018)](https://blog.agilebits.com/2018/01/25/terraforming-1password/) - the story of how 1Password switched their infrastructure config from [CloudFormation](https://aws.amazon.com/cloudformation/) to [Terraform](https://www.terraform.io/). Both are neat tools for Infrastructure as Code, but Terraform was the better tool for them. In my experience that was the case as well - writing YAML and the abstractions of Terraform are better than writing JSON and those of CF.

[An infrastructure guide for founders (2017)](https://medium.com/starting-up-security/an-infrastructure-guide-for-founders-bbada8431fb1) - this isn’t as broad as the title makes it out, but it’s still relevant. Basically, you should think from the start about how to handle application logs&monitoring, the structure of AWS/GCP accounts and the software engineering around infrastructure. Common sense advice really.
Compilers 105 - Back End Overview
Tags: compiler, llvm
Published: 2018-02-02 14:54:37

---
title: Compilers 105 - Back End Overview
series: Compilers
published: true
description: Learn about compilers and LLVM
tags: Compiler, LLVM
---

Once the front end has done its work its time for the back end components to take over.

This is the fifth my compiler series and the first on the back end. I covered the parts of the compiler that are called the front end in these posts:

[Compilers 101 – Overview and Lexer](https://dev.to/lefebvre/compilers-101---overview-and-lexer-3i0m)
[Compilers 102 – Parser](https://dev.to/lefebvre/compilers-102---parser-2gni)
[Compilers 103 – Semantic Analyzer](https://dev.to/lefebvre/compilers-103--semantic-analyzer-540k)
[Compilers 104 – IR Generation](https://dev.to/lefebvre/compilers-104---ir-generation-39e8)

## Back End
The components of the back end take the IR that was generated by the last step of the front end (IR Generation) and emit executable code, which is machine language in the case of Xojo.

The rest of the posts in this series will cover the back end as it pertains to LLVM. Specifically, the components are:

* Optimizer
* Code Generation
* Linker

Stay tuned!
What are your favorite IntelliJ shortcuts?
Tags: intellij, shortcuts, productivity, discuss
Published: 2018-02-02 14:15:47

---
title: What are your favorite IntelliJ shortcuts?
published: true
tags: intellij, shortcuts, productivity, discuss
---

I'm a shortcut junkie, I love learning new ways to take control over the tools I use, whether it is IntelliJ, VS Code, or even a browser!

That's probably because I watched _way too many_ episodes of Mr Robot.

I'm not gonna lie, it does make me feel a little bit like:

![Hackerman](https://media.giphy.com/media/RyXVu4ZW454IM/giphy.gif)
Becoming a sustaining member of dev.to is not optimal
Tags: discuss, sustainingmembership
Published: 2018-02-02 13:16:18

---
title: Becoming a sustaining member of dev.to is not optimal
published: true
tags: discuss, sustaining membership
---
I wanted to sign up and become a Sustaining Member of dev.to.

Without looking.  Say out loud where the link to do that is.

I'm not a betting man but I would guess that you were not even close.

Come on dev.to team.  Sort it out.  Front and centre.  Big call to action button.

And..... Even if you find the page - chances are you'll be disappointed that you can't just click the $10 or $25 box.

Harsh?  Maybe.  I just want the community to grow and flourish.
InstallXAMPP
Tags: php, apache, mysql, ubuntu
Published: 2018-02-02 09:21:58

---
title: "InstallXAMPP"
published: true
description: XAMPP, c’est LAMPP et tous ces services compilé. Il s’installe très rapidement, facilement et fournit un ensemble complet de services pour faire tourner des serveurs Web local en un click. 
tags: #php #apache #mysql #ubuntu
---
["Tutoriel XAMPP pour Ubuntu 16.04"](https://medium.com/@corentinwarin/tutoriel-pour-un-serveur-local-xampp-localhost-7f5642954845 "Tutoriel pour un serveur local XAMPP")
![](https://3.bp.blogspot.com/-Fy_0q2Y1AWE/UjqaT9QfgBI/AAAAAAAAA4w/dm2x7G87IEc/s320/xampp-logo.jpg "XAMPP")
What books would you recommend to a project manager that has no software development background?
Tags: question, projectmanagement, discuss
Published: 2018-02-02 01:00:36

---
title: What books would you recommend to a project manager that has no software development background?
published: true
description: I was asked by my girlfriend this question. I've written a the list that follows up but; what is essential to every project manager?
tags: question, project management, discuss
---

What would you think every project manager in the software industry should know about? Should managers really know at a very low level what a developer does? Probably not. But where should the line be drawn?

I've been thinking about this for a while now.  

What other books, papers or concepts do you recommend as a must?

### My must reads
- The mythical man month
- No Silver Bullet 

I'm sure there's a lot of room for improvement in this small list. Please don't be shy and help me to expand the list!
LowDB Blog
Published: 2021-11-19 17:29:07

---
title: LowDB Blog
published: true
date: 2018-02-02 00:00:00 UTC
tags: 
canonical_url: https://www.weckmann.me/posts/lowdb-blog/
---

LowDB Blog is a LowDB (ya don’t say?) and jQuery powered blog with a full private and public API. This repository is updated frequently. [![Remix](https://cdn.glitch.com/2bdfb3f8-05ef-4035-a06e-2043962a3a13%2Fremix%402x.png?1513093958726)](https://glitch.com/edit/#!/remix/lowdb-blog)

or

Clone on Github

```
$ git clone https://github.com/clarkhacks/LowDB-Blog.git
$ cd LowDB-Blog

```

## Setup

1. Run `npm install`
2. Set a secret in `secret.example.json`. Make it complicated, like a hard password, _(ex: 48zu-QdF5a-dS)_ with URL friendly characters.
3. Rename `secret.example.json` to “secret.json”. (In production use environmental variables)
4. Set Meta data and Posts via the API or `db.json` (In production store data in `.data/db.json`)
5. Run `node server.js`

## API

The API is relatively simple to understand and use. It is based around post requests and your `.env` secret you set earlier. Examples are shown in jQuery.

### New Post

_End Point_ `/posts/new`

_Parameters_ `title`,`date`,`body`, and `token`.

_Example_ `$.post("/posts/new", { title: "Title", date: "Month Day, body: "Lorem ipsum...", token: "TOKENHERE"});`

All queries are required.

### Meta Data

_End Point_ `/meta/:ID`

_IDs_ `user`,`bio`,`link`,`photo`, `photo`, and `title`.

_Parameters_ `value`, `text`, and `token`.

_Example_ `$.post("/meta/bio", { value: "Lorem ipsum...", token: "TOKENHERE"});`

_Example 2_ `$.post("/meta/link", { text: "Google", value: "google.com", token: "TOKENHERE"});`

All queries are required expres text. Text is used only for the `link` ID.

### Public API

The public API is a read only version of the LowDB json file.

_End Point_ `/api/v1`

* * *
 

[Reply via email](mailto:arpitbatra123@gmail.com?subject=Reply:%20LowDB%20Blog)
Help Me Refactor dev.to's Markdown Service!
Tags: meta, refactorit, ruby, help
Published: 2018-02-01 21:22:50

---
title: Help Me Refactor dev.to's Markdown Service!
published: true
description: Wanna preview our code before we open source? (NOT CLICKBAIT)
tags: meta, refactorit, ruby, help
---

So I'm trying to refactor one of our services that checks the Markdown before cleaning it up a bit more.

Here's the original:

```ruby
# instance method in article.rb
def fix_markdown
  fixed_body_markdown = MarkdownFixer.new(body_markdown).fix_all
  fixed_body_markdown.gsub!("\r\n", "\n")
  fixed_body_markdown.gsub!(/\ntags:.*\n/) do |tags|
    tags.split(" #").join(",").gsub("#", "").gsub(":,", ": ")
  end
  fixed_body_markdown
end

# markdown_fixer.rb service

class MarkdownFixer
  attr_accessor :markdown

  def initialize(markdown)
    @markdown = markdown
  end

  # This turns --- into ------- after the first two,
  # because --- messes with front matter
  def modify_hr_tags
    markdown.gsub(/^---/).with_index { |m, i| i > 1 ? "#{m}-----" : m }
  end
end
```

The goal is to put the `fix_markdown` method from `article.rb` into `MarkdownFixer.rb`, and add an additional step to add quotation marks around the title, `add_quotes_to_title`. The tricky part is that I need the return value of each `.gsub` method call, and I personally think chaining the methods over and over would not be readable.

```ruby
class MarkdownFixer
  attr_accessor :markdown

  def initialize(markdown)
    @markdown = markdown
  end

  def fix_all
    # this part is my pain point
    markdown = add_quotes_to_title
    markdown = modify_hr_tags
    markdown = markdown.gsub!("\r\n", "\n")
    markdown.gsub!(/\ntags:.*\n/) do |tags|
      tags.split(" #").join(",").gsub("#", "").gsub(":,", ": ")
    end
  end

  def add_quotes_to_title
    quoted_beginning = markdown.gsub("title: ", "title: \"")
    quoted_beginning.gsub("\r\npublished: ", "\"\r\npublished: ")
  end

  # This turns --- into ------- after the first two,
  # because --- messes with front matter
  def modify_hr_tags
    markdown.gsub(/^---/).with_index { |m, i| i > 1 ? "#{m}-----" : m }
  end
end
```

ANYWAY, I'm not too big of a fan of setting `markdown` over and over again, but I'm not sure of a better way of cleaning up the `fix_all` method. Would love to hear your thoughts.
Pure Class Fields in React Component
Tags: react, javascript
Published: 2018-02-01 20:49:26

---
title: Pure Class Fields in React Component
published: true
description: 
tags: react, JavaScript
---


With [Public Class Fields](https://tc39.github.io/proposal-class-public-fields/), you can add instance properties to the class definition with the assignment operator (=). In my opinion, it makes working with Javascript classes clean. I have implemented most of React Native Components with this public class fields. you can check it up [here](https://github.com/OMENSAH/ReactNativeComponents)
★ How to upgrade from PHP 7.1 to 7.2 on MacOS using Homebrew
Tags: php
Published: 2020-01-02 22:58:17

---
title: ★ How to upgrade from PHP 7.1 to 7.2 on MacOS using Homebrew
published: true
date: 2018-02-01 17:50:06 UTC
tags: php
canonical_url: https://freek.dev/966-how-to-upgrade-from-php-71-to-72-on-macos-using-homebrew
---

[PHP 7.2](http://php.net/manual/en/migration72.new-features.php) was released almost two months ago. I decided to wait a bit until a stable version of [Xdebug](https://xdebug.org/) with PHP 7.2 compatiblity was available. And that happened yesterday with the release of Xdebug 2.6.

I'm on MacOS and use brew to install stuff. Upgrading from PHP 7.1 to 7.2 is laughably easy. These are the steps that I performed:

```
brew tap homebrew/homebrew-php
brew unlink php71
brew install php72 --with-argon2 
brew install php72-xdebug
```

Boom! Done!

If you need imagick or redis support, execute these two commands:

```
brew install php72-imagick
brew install php72-redis
```

And that's all there is to it.

![terminal](https://freek.dev/uploads/media/php72/terminal.png)
I Lost $30,000 Because I Didn't Listen To A Less Experienced Developer
Tags: webdev, beginnings
Published: 2018-02-01 16:47:43

---
title: I Lost $30,000 Because I Didn't Listen To A Less Experienced Developer
published: true
tags: webdev, beginnings
---

It took me a long time to learn to be a good developer. I thought just because I knew a little something about code, I was already a good developer. I later made a huge mistake that costs the company I was working for $30,000 dollars. I remember when I first was told that I cost my company $30,000.00, I got knocked down a peg or two. I realized that I was not that great of the developer I had put in my head.

*What happened?*

I was assigned to a project as the lead web developer. The project was to create a user signup portal that would allow the users to come back and watch a "live" (recorded) webinar at a future date and time. On the date of the webinar, the stream went live and crashed the service hosting the pre-recorded "live" webinar.

Three weeks earlier a less experience web developer had suggested that we load test the streaming service to make sure it can handle the expected load we were expected during the webinar. Of course, I didn't listen to him and ignored his request to load test.

In the end, the company I was working for had to refund the entire cost of the project to the people who hired them. **$30,000.00**

*Want to read more about what happened?*

To read more about all of the mistakes I made as an up and coming web developer, read my latest post ["How To Become A Web Developer, The Wrong Way"](https://ua1.us/news/developer-tips/how-to-become-a-web-developer/) over at [UA1 Labs](https://ua1.us)
Building Real-Time Web Applications using wolkenkit
Tags: javascript, node, wolkenkit
Published: 2018-02-01 16:44:13

---
title: Building Real-Time Web Applications using wolkenkit
published: true
description: 
tags: #javascript #nodejs #wolkenkit
canonical_url: https://auth0.com/blog/building-real-time-web-applications-using-wolkenkit/
---

Learn how to build real-time web applications using Wolkenkit, a CQRS and event-sourcing framework for JavaScript and Node.js, that perfectly matches DDD.

[Read on 🎢](https://auth0.com/blog/building-real-time-web-applications-using-wolkenkit/?utm_source=dev&utm_medium=sc&utm_campaign=wolkenkit)


![Building Real-Time Web Applications using wolkenkit](https://thepracticaldev.s3.amazonaws.com/i/701oli7p3h3vf1fpycac.png)