Issue: Packet sequence number wrong - got 2 expected 1 to due to PyMySQL
Author: kadnan
State: closed
Created: 2017-06-19 06:58:32

I am using `PyMySQL` and they have discussed this problem [here](https://github.com/PyMySQL/PyMySQL/issues/422). They asked to set `threadsafety`. How to change value of `threadsafety`, if that is the issue? How do I do it in `peewee` as my app is multi-process and having a pool of two processes like this:

```
with Pool(2) as p:
            result = p.map(parse, links)
```

**Update**

My Code Look like this:

```
from peewee import *
import datetime
import xmltodict

settings = None
with open('../settings.xml') as f:
    settings = f.read()

config = xmltodict.parse(settings)

user = config['config']['database']['user']
password = config['config']['database']['password']
db_name = config['config']['database']['name']
host = config['config']['database']['host']

dbhandle = MySQLDatabase(
    db_name, user=user,
    password=password,
    host=host
)

dbhandle.connect()

class BaseModel(Model):
    class Meta:
        database = dbhandle


class Links(BaseModel):
    id = PrimaryKeyField(null=False)
    url = CharField(max_length=255, default='')
    duration = IntegerField(default=0)
    status = IntegerField(
        default=0)  # 
    updated_at = DateTimeField(default=datetime.datetime.now)

    class Meta:
        db_table = "my_links"
        order_by = ('updated_at',)
```

**MySQL Threads status**

```
mysql> show global status like '%thread%';
+------------------------------------------+-------+
| Variable_name                            | Value |
+------------------------------------------+-------+
| AuroraDb_thread_deadlocks                | 0     |
| Delayed_insert_threads                   | 0     |
| Performance_schema_thread_classes_lost   | 0     |
| Performance_schema_thread_instances_lost | 0     |
| Slow_launch_threads                      | 0     |
| Threads_cached                           | 0     |
| Threads_connected                        | 5     |
| Threads_created                          | 10    |
| Threads_running                          | 1     |
+------------------------------------------+-------+
9 rows in set (0.00 sec)

mysql> show global variables like '%thread%';
+-----------------------------------------+---------------------------------+
| Variable_name                           | Value                           |
+-----------------------------------------+---------------------------------+
| innodb_purge_threads                    | 1                               |
| innodb_read_io_threads                  | 4                               |
| innodb_thread_concurrency               | 0                               |
| innodb_thread_sleep_delay               | 10000                           |
| innodb_write_io_threads                 | 4                               |
| max_delayed_threads                     | 20                              |
| max_insert_delayed_threads              | 20                              |
| myisam_repair_threads                   | 1                               |
| performance_schema_max_thread_classes   | 50                              |
| performance_schema_max_thread_instances | -1                              |
| thread_cache_size                       | 0                               |
| thread_concurrency                      | 10                              |
| thread_handling                         | multiple-connections-per-thread |
| thread_stack                            | 262144                          |
+-----------------------------------------+---------------------------------+
14 rows in set (0.00 sec)
```

If you're trying to pass arguments into the pymysql constructor, just pass them in as keyword arguments to the `MySQLDatabase` initialization method. They'll get passed back to the database driver.

e.g. `MySQLDatabase(..., threadsafety=1)`

@coleifer 

Got exception:

```
File "/anaconda3/anaconda/lib/python3.5/site-packages/peewee.py", line 3708, in _create_connection
    return self._connect(self.database, **self.connect_kwargs)
  File "/anaconda3/anaconda/lib/python3.5/site-packages/peewee.py", line 4256, in _connect
    return mysql.connect(db=database, **conn_kwargs)
  File "/anaconda3/anaconda/lib/python3.5/site-packages/pymysql/__init__.py", line 90, in Connect
    return Connection(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'threadsafety'
```

This is how I did:

```
dbhandle = MySQLDatabase(
    db_name, user=user,
    password=password,
    host=host,
    threadsafety=3
)
```

I have no idea then, but whatever the case, the issue is not with Peewee.

@kadnan 您好,我也遇到了这样的问题,请问您是怎么解决的呢？